| @@ -115,3 +115,5 @@ Alogrithm/Alogrithm/Debug/9_AddBinary.obj | |||
| Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/27a527fda8b54c6b/9_ADDBINARY.ipch | |||
| Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/d00489e06883ba57/9_ ADDBINARY.ipch | |||
| Alogrithm/TestResults/dc42f77d-a2f7-41a9-8b8c-1ee3f1a82eb9/林_MI-1 2021-01-18 14_12_23.coverage | |||
| Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/86fa7a487ad3f37c/10_BINARYTREEPATHS.ipch | |||
| Alogrithm/Alogrithm/Debug/10_BinaryTreePaths.obj | |||
| @@ -86,8 +86,8 @@ | |||
| <ClCompile> | |||
| <WarningLevel>Level3</WarningLevel> | |||
| <SDLCheck>true</SDLCheck> | |||
| <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
| <ConformanceMode>true</ConformanceMode> | |||
| <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
| <ConformanceMode>false</ConformanceMode> | |||
| </ClCompile> | |||
| <Link> | |||
| <SubSystem>Console</SubSystem> | |||
| @@ -140,6 +140,7 @@ | |||
| </Link> | |||
| </ItemDefinitionGroup> | |||
| <ItemGroup> | |||
| <ClCompile Include="src\10_BinaryTreePaths.cpp" /> | |||
| <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | |||
| <ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | |||
| <ClCompile Include="src\3_bool IsUgly.cpp" /> | |||
| @@ -152,6 +153,7 @@ | |||
| <ClCompile Include="src\main.cpp" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="include\10_BinaryTreePaths.h" /> | |||
| <ClInclude Include="include\1_ContainsNearbyDuplicate.h" /> | |||
| <ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | |||
| <ClInclude Include="include\3_bool IsUgly.h" /> | |||
| @@ -163,6 +165,7 @@ | |||
| <ClInclude Include="include\9_AddBinary.h" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <None Include="config\10_BinaryTreePaths.ini" /> | |||
| <None Include="config\1_ContainsNearbyDuplicate.ini" /> | |||
| <None Include="config\2_ExcelSheetColumnTiTle.ini" /> | |||
| <None Include="config\3_bool IsUgly.ini" /> | |||
| @@ -54,6 +54,9 @@ | |||
| <ClCompile Include="src\9_AddBinary.cpp"> | |||
| <Filter>源文件\src</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="src\10_BinaryTreePaths.cpp"> | |||
| <Filter>源文件\src</Filter> | |||
| </ClCompile> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | |||
| @@ -83,6 +86,9 @@ | |||
| <ClInclude Include="include\9_AddBinary.h"> | |||
| <Filter>头文件\include</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="include\10_BinaryTreePaths.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <None Include="config\3_bool IsUgly.ini"> | |||
| @@ -112,5 +118,8 @@ | |||
| <None Include="config\9_AddBinary.ini"> | |||
| <Filter>资源文件\config</Filter> | |||
| </None> | |||
| <None Include="config\10_BinaryTreePaths.ini"> | |||
| <Filter>资源文件\config</Filter> | |||
| </None> | |||
| </ItemGroup> | |||
| </Project> | |||
| @@ -0,0 +1,18 @@ | |||
| struct TreeNode3 | |||
| { | |||
| int val; | |||
| struct TreeNode3* left; | |||
| struct TreeNode3* right; | |||
| }; | |||
| #pragma once | |||
| #include <atlstr.h> | |||
| void CreatBitTreeNode3(char str[][50], int return_count, TreeNode3* cur, int curIndex); | |||
| TreeNode3* CreatBitTree3(char str[][50], int return_count); | |||
| void get_path(char** array, struct TreeNode3* root, int* returnSize, int* buf, int local); | |||
| char** binaryTreePaths(struct TreeNode3* root, int* returnSize); | |||
| void free_tree3(TreeNode3* T); | |||
| int str_device2(CString str, char(*return_str)[50]); | |||
| @@ -0,0 +1,106 @@ | |||
| #include "../include/10_BinaryTreePaths.h" | |||
| void CreatBitTreeNode3(char str[][50], int return_count, TreeNode3* cur, int curIndex) | |||
| { | |||
| if (str == NULL || return_count <= 0 || cur == NULL || curIndex >= return_count || curIndex < 0) { | |||
| return; | |||
| } | |||
| int last = return_count - 1; | |||
| if ((2 * curIndex + 1 <= last) && strcmp(str[2 * curIndex + 1], "NULL")) { | |||
| cur->left = (TreeNode3*)malloc(sizeof(TreeNode3)); | |||
| if (NULL == cur->left) { | |||
| return; | |||
| } | |||
| cur->left->val = _ttoi(str[2 * curIndex + 1]); | |||
| } | |||
| else { | |||
| cur->left = NULL; | |||
| } | |||
| if ((2 * curIndex + 2 <= last) && strcmp(str[2 * curIndex + 2], "NULL")) { | |||
| cur->right = (TreeNode3*)malloc(sizeof(TreeNode3)); | |||
| if (NULL == cur->right) { | |||
| return; | |||
| } | |||
| cur->right->val = _ttoi(str[2 * curIndex + 2]); | |||
| } | |||
| else { | |||
| cur->right = NULL; | |||
| } | |||
| CreatBitTreeNode3(str, return_count, cur->left, 2 * curIndex + 1); | |||
| CreatBitTreeNode3(str, return_count, cur->right, 2 * curIndex + 2); | |||
| } | |||
| TreeNode3* CreatBitTree3(char str[][50], int return_count) | |||
| { | |||
| if (str == NULL || return_count <= 0) { | |||
| return NULL; | |||
| } | |||
| TreeNode3* head = (TreeNode3*)malloc(sizeof(TreeNode3)); | |||
| if (NULL == head) { | |||
| return NULL; | |||
| } | |||
| head->val = _ttoi(str[0]); | |||
| CreatBitTreeNode3(str, return_count, head, 0); | |||
| return head; | |||
| } | |||
| void get_path(char** array, struct TreeNode3* root, int* returnSize, int* buf, int local) | |||
| { | |||
| if (NULL == root) { | |||
| return; | |||
| } | |||
| if (!root->left && !root->right) { | |||
| char* str = (char*)malloc(1024); | |||
| int len = 0; | |||
| for (int i = 0; i < local; i++) | |||
| { | |||
| len += sprintf(str + len, "%d->", buf[i]); | |||
| } | |||
| sprintf(str + len, "%d", root->val); | |||
| array[(*returnSize)++] = str; | |||
| } | |||
| else { | |||
| buf[local++] = root->val; | |||
| get_path(array, root->left, returnSize, buf, local); | |||
| get_path(array, root->right, returnSize, buf, local); | |||
| } | |||
| } | |||
| char** binaryTreePaths(struct TreeNode3* root, int* returnSize) { | |||
| char** ret = (char**)malloc(sizeof(char*) * 1024);//定义一个二级指针,作为返回值返回二维字符串数组 | |||
| *returnSize = 0;//用来保存二维字符串数组的元素个数 | |||
| int buf[1024] = { 0 };//用来接收缓冲 | |||
| get_path(ret, root, returnSize, buf, 0); | |||
| return ret; | |||
| } | |||
| void free_tree3(TreeNode3* T)//后序释放 | |||
| { | |||
| if (!T) { | |||
| return; | |||
| } | |||
| if (T->left) { | |||
| free_tree3(T->left); | |||
| } | |||
| if (T->right) { | |||
| free_tree3(T->right); | |||
| } | |||
| if (T) { | |||
| free(T); | |||
| T = NULL; | |||
| } | |||
| } | |||
| int str_device2(CString str, char(*return_str)[50]) | |||
| { | |||
| int value_count = 0; | |||
| char* token; //存放被切割后的第一个子串 | |||
| char Section_value[500] = { 0 };//存放nums转换成string类型的结果 | |||
| memset(Section_value, 0, sizeof(char) * 500); | |||
| strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串 | |||
| //获得切割到的第一个字符串 | |||
| token = strtok(Section_value, ","); | |||
| /* 继续获取其他的子字符串 */ | |||
| while (token != NULL) { | |||
| strcpy(return_str[value_count], token); | |||
| token = strtok(NULL, ","); | |||
| value_count++; //记录存了多少个元素 | |||
| } | |||
| return value_count; //返回数组首地址 | |||
| } | |||
| @@ -1,8 +1,33 @@ | |||
| #include "../include/2_ExcelSheetColumnTitle.h" | |||
| #include "../include/3_bool IsUgly.h" | |||
| #include "../include/10_BinaryTreePaths.h" | |||
| int main() | |||
| { | |||
| //ExcelSheetColumnTitle(10); | |||
| //printf("%d\n", IsUgly(6)); | |||
| CString str = "3,9,20,NULL,NULL,15,17"; | |||
| CString str1 = "4,3,1,NULL,NULL,2,6,8"; | |||
| char return_str[100][50] = { 0 }; | |||
| int return_count = str_device2(str, return_str); | |||
| TreeNode3* root = CreatBitTree3(return_str, return_count); | |||
| int returnSize = 0; | |||
| char** returnStr; | |||
| char s[1024] = {0}; | |||
| char *q = "123"; | |||
| char* p = "123"; | |||
| returnStr = binaryTreePaths(root, &returnSize); | |||
| for (int i = 0; i < returnSize; i++) | |||
| { | |||
| //sprintf(s, "%s", returnStr[i]); | |||
| strcat(s, returnStr[i]); | |||
| if (i != (returnSize-1)) | |||
| { | |||
| s[strlen(s)] = ','; | |||
| } | |||
| } | |||
| printf("%s\n", s); | |||
| free_tree3(root); | |||
| return 0; | |||
| } | |||