@@ -30,7 +30,7 @@ | |||||
<ConfigurationType>Application</ConfigurationType> | <ConfigurationType>Application</ConfigurationType> | ||||
<UseDebugLibraries>true</UseDebugLibraries> | <UseDebugLibraries>true</UseDebugLibraries> | ||||
<PlatformToolset>v142</PlatformToolset> | <PlatformToolset>v142</PlatformToolset> | ||||
<CharacterSet>Unicode</CharacterSet> | |||||
<CharacterSet>MultiByte</CharacterSet> | |||||
</PropertyGroup> | </PropertyGroup> | ||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||||
<ConfigurationType>Application</ConfigurationType> | <ConfigurationType>Application</ConfigurationType> | ||||
@@ -143,12 +143,14 @@ | |||||
<ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | ||||
<ClInclude Include="include\3_IsUgly.h" /> | <ClInclude Include="include\3_IsUgly.h" /> | ||||
<ClInclude Include="include\4_IsPalindrome.h" /> | <ClInclude Include="include\4_IsPalindrome.h" /> | ||||
<ClInclude Include="include\5_MinDepth.h" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | ||||
<ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | <ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | ||||
<ClCompile Include="src\3_IsUgly.cpp" /> | <ClCompile Include="src\3_IsUgly.cpp" /> | ||||
<ClCompile Include="src\4_IsPalindrome.cpp" /> | <ClCompile Include="src\4_IsPalindrome.cpp" /> | ||||
<ClCompile Include="src\5_MinDepth.cpp" /> | |||||
<ClCompile Include="src\main.cpp" /> | <ClCompile Include="src\main.cpp" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
@@ -156,6 +158,7 @@ | |||||
<None Include="config\2_ExcelSheetColumnTiTle.ini" /> | <None Include="config\2_ExcelSheetColumnTiTle.ini" /> | ||||
<None Include="config\3_IsUgly.ini" /> | <None Include="config\3_IsUgly.ini" /> | ||||
<None Include="config\4_IsPalindrome.ini" /> | <None Include="config\4_IsPalindrome.ini" /> | ||||
<None Include="config\5_MinDepth.ini" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
<ImportGroup Label="ExtensionTargets"> | <ImportGroup Label="ExtensionTargets"> | ||||
@@ -27,6 +27,9 @@ | |||||
<ClInclude Include="include\4_IsPalindrome.h"> | <ClInclude Include="include\4_IsPalindrome.h"> | ||||
<Filter>头文件</Filter> | <Filter>头文件</Filter> | ||||
</ClInclude> | </ClInclude> | ||||
<ClInclude Include="include\5_MinDepth.h"> | |||||
<Filter>头文件</Filter> | |||||
</ClInclude> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<ClCompile Include="src\1_ContainsNearbyDuplicate.cpp"> | <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp"> | ||||
@@ -44,6 +47,9 @@ | |||||
<ClCompile Include="src\4_IsPalindrome.cpp"> | <ClCompile Include="src\4_IsPalindrome.cpp"> | ||||
<Filter>源文件</Filter> | <Filter>源文件</Filter> | ||||
</ClCompile> | </ClCompile> | ||||
<ClCompile Include="src\5_MinDepth.cpp"> | |||||
<Filter>源文件</Filter> | |||||
</ClCompile> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="config\1_ContainsNearbyDuplicate.ini"> | <None Include="config\1_ContainsNearbyDuplicate.ini"> | ||||
@@ -58,5 +64,8 @@ | |||||
<None Include="config\4_IsPalindrome.ini"> | <None Include="config\4_IsPalindrome.ini"> | ||||
<Filter>资源文件</Filter> | <Filter>资源文件</Filter> | ||||
</None> | </None> | ||||
<None Include="config\5_MinDepth.ini"> | |||||
<Filter>资源文件</Filter> | |||||
</None> | |||||
</ItemGroup> | </ItemGroup> | ||||
</Project> | </Project> |
@@ -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 |
@@ -0,0 +1,16 @@ | |||||
#pragma once | |||||
#include <stdio.h> | |||||
#include <math.h> | |||||
#include <atlstr.h> | |||||
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); |
@@ -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; | |||||
} | |||||
} |
@@ -62,6 +62,23 @@ int* str_device(CString str, int* value_count) | |||||
} | } | ||||
return Section_devide; //返回数组首地址 | 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 | ||||
bool CstrToBool(CString str) | bool CstrToBool(CString str) | ||||
{ | { | ||||
@@ -18,10 +18,12 @@ | |||||
#include"../code/include/2_ExcelSheetColumnTitle.h" | #include"../code/include/2_ExcelSheetColumnTitle.h" | ||||
#include"../code/include/3_IsUgly.h" | #include"../code/include/3_IsUgly.h" | ||||
#include"../code/include/4_IsPalindrome.h" | #include"../code/include/4_IsPalindrome.h" | ||||
#include"../code/include/5_MinDepth.h" | |||||
int CalcCount(int n, char(*str)[10], const char* FileName); | int CalcCount(int n, char(*str)[10], const char* FileName); | ||||
int* str_device(CString str, int* value_count); | int* str_device(CString str, int* value_count); | ||||
int str_device2(CString str, char(*return_str)[50]); | |||||
bool CstrToBool(CString str); | bool CstrToBool(CString str); | ||||
@@ -6,6 +6,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; | |||||
#define FileName_2 "../code/config/2_ExcelSheetColumnTiTle.ini" | #define FileName_2 "../code/config/2_ExcelSheetColumnTiTle.ini" | ||||
#define FileName_3 "../code/config/3_IsUgly.ini" | #define FileName_3 "../code/config/3_IsUgly.ini" | ||||
#define FileName_4 "../code/config/4_IsPalindrome.ini" | #define FileName_4 "../code/config/4_IsPalindrome.ini" | ||||
#define FileName_5 "../code/config/5_MinDepth.ini" | |||||
namespace test1 | 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); | |||||
} | |||||
} | |||||
}; | |||||
} |