diff --git a/.gitignore b/.gitignore index 4f84f06..df973dd 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,7 @@ Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/ab4a44a8d749bee1/6_CONTAINSDUPLICATE.ip >>>>>>> 71fa9786fe524fa393358479cab8105f9239c68c Alogrithm/Alogrithm/Debug/6_ContainsDuplicate.obj Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini.rej +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/8d72cbe9b24557ef/7_MAXDEPTH.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/d67b185b0905d0e0/7_MAXDEPTH.ipch +Alogrithm/.vs/Alogrithm/v16/TestStore/3/001.testlog +Alogrithm/Alogrithm/Debug/7_MaxDepth.obj diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 88ad43f..b3b564a 100644 Binary files a/Alogrithm/.vs/Alogrithm/v16/.suo and b/Alogrithm/.vs/Alogrithm/v16/.suo differ diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj b/Alogrithm/Alogrithm/Alogrithm.vcxproj index 0733cea..cb9a2f8 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj @@ -146,6 +146,7 @@ + @@ -155,6 +156,7 @@ + @@ -163,6 +165,7 @@ + diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters index 34e6ca5..9c8452f 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters @@ -45,6 +45,9 @@ 源文件\src + + 源文件\src + @@ -65,6 +68,9 @@ 头文件\include + + 头文件\include + @@ -85,5 +91,8 @@ 资源文件\config + + 资源文件\config + \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/7_MaxDepth.ini b/Alogrithm/Alogrithm/config/7_MaxDepth.ini new file mode 100644 index 0000000..c0820a8 --- /dev/null +++ b/Alogrithm/Alogrithm/config/7_MaxDepth.ini @@ -0,0 +1,15 @@ +[Test1] +input=3,9,20,NULL,NULL,15,7 +output=3 +[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 diff --git a/Alogrithm/Alogrithm/include/7_MaxDepth.h b/Alogrithm/Alogrithm/include/7_MaxDepth.h new file mode 100644 index 0000000..3299e7b --- /dev/null +++ b/Alogrithm/Alogrithm/include/7_MaxDepth.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + + +struct TreeNode2 +{ + int val; + struct TreeNode2* left; + struct TreeNode2* right; +}; + + +int MaxDepth(struct TreeNode2* root); +TreeNode2* CreatBitTree2(char str[][50], int return_count); +void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* cur, int curIndex); +void free_tree2(TreeNode2* T); diff --git a/Alogrithm/Alogrithm/src/7_MaxDepth.cpp b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp new file mode 100644 index 0000000..52af908 --- /dev/null +++ b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp @@ -0,0 +1,83 @@ +#include "../include/7_MaxDepth.h" + +TreeNode2* CreatBitTree2(char str[][50], int return_count) +{ + if (str == NULL || return_count <= 0) { + return NULL; + } + TreeNode2* head = (TreeNode2*)malloc(sizeof(TreeNode2)); + if (NULL == head) { + return NULL; + } + head->val = _ttoi(str[0]); + CreatBitTreeNode2(str, return_count, head, 0); + return head; +} +void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* 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 = (TreeNode2*)malloc(sizeof(TreeNode2)); + 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 = (TreeNode2*)malloc(sizeof(TreeNode2)); + if (NULL == cur->right) { + return; + } + cur->right->val = _ttoi(str[2 * curIndex + 2]); + } + else { + cur->right = NULL; + } + CreatBitTreeNode2(str, return_count, cur->left, 2 * curIndex + 1); + CreatBitTreeNode2(str, return_count, cur->right, 2 * curIndex + 2); +} +int MaxDepth(struct TreeNode2* root) +{ + int Depth = 0; + int L1, L2; + if (NULL == root) { + return 0; + } + else if ((NULL == root->left) && (NULL == root->right)) { + return 1; + } + else if ((NULL != root->left) && (NULL == root->right)) { + Depth = MaxDepth(root->left); + } + else if ((NULL == root->left) && (NULL != root->right)) { + Depth = MaxDepth(root->right); + } + else { + L1 = MaxDepth(root->left); + L2 = MaxDepth(root->right); + Depth = fmax(L1, L2); + } + return Depth + 1; +} +void free_tree2(TreeNode2* T)//ͷ +{ + if (!T) { + return; + } + if (T->left) { + free_tree2(T->left); + } + if (T->right) { + free_tree2(T->right); + } + if (T) { + free(T); + T = NULL; + } +} \ No newline at end of file diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp index 68f3a8b..86c93e6 100644 --- a/Alogrithm/UnitTest/UnitTest.cpp +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -8,6 +8,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" #define FileName_5 "../Alogrithm/config/5_MinDepth.ini" #define FileName_6 "../Alogrithm/config/6_ContainsDuplicate.ini" +#define FileName_7 "../Alogrithm/config/7_MaxDepth.ini" namespace UnitTest { @@ -112,4 +113,23 @@ namespace UnitTest } } }; + TEST_CLASS(UnitTest_7) + { + TEST_METHOD(TestMethode1) + { + char Section_Name[100][10] = { 0 }; + int Section_Count = CalcCount(100, Section_Name, FileName_7); + CString Na, nExpect; + for (int i = 0; i < Section_Count; i++) { + GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_7); + GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_7); + char return_str[100][50] = { 0 }; + int return_count = str_device2(Na, return_str); + TreeNode2* root = CreatBitTree2(return_str, return_count); + int nReal = MaxDepth(root); + Assert::AreEqual(nReal, _ttoi(nExpect)); + free_tree2(root); + } + } + }; } diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj index 5284fc8..9573ad6 100644 --- a/Alogrithm/UnitTest/UnitTest.vcxproj +++ b/Alogrithm/UnitTest/UnitTest.vcxproj @@ -103,7 +103,7 @@ Windows $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) DebugFull - ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;%(AdditionalDependencies) + ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;%(AdditionalDependencies) diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h index 55a32ff..63a8b83 100644 --- a/Alogrithm/UnitTest/pch.h +++ b/Alogrithm/UnitTest/pch.h @@ -19,7 +19,7 @@ #include"../Alogrithm/include/4_IsPalindrome.h" #include"../Alogrithm/include/5_MinDepth.h" #include"../Alogrithm/include/6_ContainsDuplicate.h" - +#include"../Alogrithm/include/7_MaxDepth.h" int CalcCount(int n, char(*str)[10],const char *FileName);