diff --git a/.vs/Algorithm/v16/.suo b/.vs/Algorithm/v16/.suo index a3c70ec..11a5fac 100644 Binary files a/.vs/Algorithm/v16/.suo and b/.vs/Algorithm/v16/.suo differ diff --git a/code/code.vcxproj b/code/code.vcxproj index fa288fe..23c4819 100644 --- a/code/code.vcxproj +++ b/code/code.vcxproj @@ -30,7 +30,7 @@ Application true v142 - Unicode + MultiByte Application @@ -143,12 +143,14 @@ + + @@ -156,6 +158,7 @@ + diff --git a/code/code.vcxproj.filters b/code/code.vcxproj.filters index 72a0c19..598896c 100644 --- a/code/code.vcxproj.filters +++ b/code/code.vcxproj.filters @@ -27,6 +27,9 @@ 头文件 + + 头文件 + @@ -44,6 +47,9 @@ 源文件 + + 源文件 + @@ -58,5 +64,8 @@ 资源文件 + + 资源文件 + \ No newline at end of file diff --git a/code/config/5_MinDepth.ini b/code/config/5_MinDepth.ini new file mode 100644 index 0000000..38a983a --- /dev/null +++ b/code/config/5_MinDepth.ini @@ -0,0 +1,33 @@ +[Test1] +input=3,9,20,NULL,NULL,15,7 +output=2 +[Test2] +input=4,3,1,2,NULL,6,8 +output=3 +[Test3] +input=4,3,1,NULL,2,6,8 +output=3 +[Test5] +input=NULL +output=1 +[Test6] +input= +output=0 +[Test7] +input=1,0,0,2,NULL,NULL,0 +output=3 +[Test8] +input=1,3,5,7,9,2,4,6,8,10,12,14,16,18,10,NULL,NULL,NULL,NULL,22 +output=4 +[Test9] +input=1,3,5,7,9,NULL,NULL,2,NULL,4,NULL +output=2 +[Test10] +input=1,3,NULL,5,7 +output=3 +[Test11] +input=23,21,22,NULL,1,2,NULL +output=3 +[Test12] +input=2,NULL,3 +output=2 \ No newline at end of file diff --git a/code/include/5_MinDepth.h b/code/include/5_MinDepth.h new file mode 100644 index 0000000..3c5f803 --- /dev/null +++ b/code/include/5_MinDepth.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +struct TreeNode +{ + int val; + struct TreeNode* left; + struct TreeNode* right; +}; + +int MinDepth(struct TreeNode* root); +TreeNode* CreatBitTree(char str[][50], int return_count); +void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curIndex); +void free_tree(TreeNode* T); diff --git a/code/src/5_MinDepth.cpp b/code/src/5_MinDepth.cpp new file mode 100644 index 0000000..b74ea26 --- /dev/null +++ b/code/src/5_MinDepth.cpp @@ -0,0 +1,90 @@ +#include "../include/5_MinDepth.h" +//ĿһС +//˼·1.㷨ȲңֱΪգ㷵Depth + 1 +// 2.ȼΪL1ȼΪL2Ȼ󷵻ؽСһ + +int MinDepth(struct TreeNode* root) +{ + int Depth = 0; + int L1, L2; + if (NULL == root) { //ڵΪգ0 + return 0; + } //Ϊգ1 + else if ((NULL == root->left) && (NULL == root->right)) { + return 1; + } //ΪգΪգ + else if ((NULL != root->left) && (NULL == root->right)) { + Depth = MinDepth(root->left); + } //ΪգΪգ + else if ((NULL == root->left) && (NULL != root->right)) { + Depth = MinDepth(root->right); + } //Ϊգ + else { + L1 = MinDepth(root->left); + L2 = MinDepth(root->right); + Depth = fmin(L1, L2);//УȽСһDepth + } + return Depth + 1;//ÿ꣬Ľۼ +} + +//һöΪַ顣 +TreeNode* CreatBitTree(char str[][50], int return_count) +{ //ַΪգ򷵻ؿ + if (str == NULL || return_count <= 0) { + return NULL; + }//ڵ㿪һƬռ + TreeNode* head = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == head) {//ڴʧܣNULL + return NULL; + }//ڵֵ + head->val = _ttoi(str[0]); + CreatBitTreeNode1(str, return_count, head, 0);//ͨúڵֵ + return head;//ظڵ +} +//ǸڵֵΪַ顢ڵ㡢־λ +void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* 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 = (TreeNode*)malloc(sizeof(TreeNode)); + 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 = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == cur->right) { + return; + } + cur->right->val = _ttoi(str[2 * curIndex + 2]); + } + else { + cur->right = NULL; + } + CreatBitTreeNode1(str, return_count, cur->left, 2 * curIndex + 1); + CreatBitTreeNode1(str, return_count, cur->right, 2 * curIndex + 2); +} +//ͷŶ +void free_tree(TreeNode* T)//ͷ +{ + if (!T) { + return; + } + if (T->left) { + free_tree(T->left); + } + if (T->right) { + free_tree(T->right); + } + if (T) { + free(T); + T = NULL; + } +} \ No newline at end of file diff --git a/test/pch.cpp b/test/pch.cpp index 5417649..2750e79 100644 --- a/test/pch.cpp +++ b/test/pch.cpp @@ -62,6 +62,23 @@ int* str_device(CString str, int* value_count) } return Section_devide; //返回数组首地址 } +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; //返回数组首地址 +} //字符串转bool bool CstrToBool(CString str) { diff --git a/test/pch.h b/test/pch.h index 8f8b714..ae5fcba 100644 --- a/test/pch.h +++ b/test/pch.h @@ -18,10 +18,12 @@ #include"../code/include/2_ExcelSheetColumnTitle.h" #include"../code/include/3_IsUgly.h" #include"../code/include/4_IsPalindrome.h" +#include"../code/include/5_MinDepth.h" int CalcCount(int n, char(*str)[10], const char* FileName); int* str_device(CString str, int* value_count); +int str_device2(CString str, char(*return_str)[50]); bool CstrToBool(CString str); diff --git a/test/test.cpp b/test/test.cpp index 31600fe..e870995 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -6,6 +6,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_2 "../code/config/2_ExcelSheetColumnTiTle.ini" #define FileName_3 "../code/config/3_IsUgly.ini" #define FileName_4 "../code/config/4_IsPalindrome.ini" +#define FileName_5 "../code/config/5_MinDepth.ini" namespace test1 @@ -84,4 +85,25 @@ namespace test4 } }; } - +namespace test5 +{ + TEST_CLASS(UnitTest_5) + { + TEST_METHOD(TestMethode1) + { + char Section_Name[100][10] = { 0 }; + int Section_Count = CalcCount(100, Section_Name, FileName_5); + CString Na, nExpect; + for (int i = 0; i < Section_Count; i++) { + GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_5); + GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_5); + char return_str[100][50] = { 0 }; + int return_count = str_device2(Na, return_str); + TreeNode* root = CreatBitTree(return_str, return_count); + int nReal = MinDepth(root); + Assert::AreEqual(nReal, _ttoi(nExpect)); + free_tree(root); + } + } + }; +}