From 2480f00863b8dfa8ea49c198db9429a782d97d2a Mon Sep 17 00:00:00 2001 From: linyongji Date: Wed, 3 Feb 2021 16:58:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=94=AF5=E6=95=B4=E7=90=86=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=8C=E6=8F=90=E4=BA=A4=E5=AE=8C=E6=88=90=E7=9A=84?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vs/Algorithm/v16/.suo | Bin 61952 -> 57344 bytes code/code.vcxproj | 5 ++- code/code.vcxproj.filters | 9 ++++ code/config/5_MinDepth.ini | 33 ++++++++++++++ code/include/5_MinDepth.h | 16 +++++++ code/src/5_MinDepth.cpp | 90 +++++++++++++++++++++++++++++++++++++ test/pch.cpp | 17 +++++++ test/pch.h | 2 + test/test.cpp | 24 +++++++++- 9 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 code/config/5_MinDepth.ini create mode 100644 code/include/5_MinDepth.h create mode 100644 code/src/5_MinDepth.cpp diff --git a/.vs/Algorithm/v16/.suo b/.vs/Algorithm/v16/.suo index a3c70ec1cb8fa8eb01522ee22c96ee4705dfdefb..11a5fac1fcf30bcde569a851cf98d6c47758fc3c 100644 GIT binary patch delta 5786 zcmeI0dr*^C7Qnxo2PQEf$XlQyq9BBj5D1_=0uAVbK(JVm+LnT}P!&*u^+6@tmRi?E z$gx^m-D+yr)p1(k({-yBgHonex9X~`ot^IDw(GRrnbn=45A0U zNvO5Vg;k9+*n`||JH|4!d@LI z0c9FOj(GqQh4>(W2z}&gHXP=X6ET+|sYnPSL6pcuM1f30=$rAFmm^waGD1^|My7IH zzN0}n7GjZjBo4_$k`eki)Rm8O<)yBC3FZ|pc^J-nhrysEfEht9jZcmg^wa^SIar?S zp_||#$GBvul}0Bl#=5HbTE5-tf5dZS!01Ic6-!3}==PCM7W^sT@LW#n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B!=DRK(}H2xfWEY2xiM#uP&7vP`DG24%b?{ce~sEFvpQM0QM47H*xkzG`Q-zo&n zWeAa#_)SK@)T5MS60s;T z0wg0Y;0U3x;bIh{7r~o&ogaE$5Ehg{cZveBp#a2R{(w2bfs0m1ep|_3>v=ZN*tSEq zF)iQ?v-mC3-xw@;J^{36g6?|9+{>2o!F0)kuCiUQt<9fjdTndTV>4iUy7*rTz-$wu zbW1Jubxs87#v*$~!?I>;(<;2&S2$t7mc$t~X*&3HUQ4JUU8D1&_clcD{4hfH!Jh8- zJ@CfInYd~-q@Lf$-h~@qC4r`N6P^S;vNz&hRzuN+Xy!()7WRLXfHm|u-XrYtvkPjp QoBSX0t9A4e`x&kEzmBr&=l}o! 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); + } + } + }; +}