Skip to content

Commit

Permalink
Initial commit for question 54
Browse files Browse the repository at this point in the history
  • Loading branch information
zhedahht committed Nov 26, 2016
1 parent 0cfc7be commit c23f6a9
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 0 deletions.
155 changes: 155 additions & 0 deletions 54_KthNodeInBST/54_KthNodeInBST.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{050F6A72-5004-42F6-8E1A-BA825C6C845B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>My54_KthNodeInBST</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="KthNodeInBST.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Utilities\Utilities.vcxproj">
<Project>{732da13a-f8bb-4cef-b96d-61de60bdb6a7}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions 54_KthNodeInBST/54_KthNodeInBST.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="KthNodeInBST.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
188 changes: 188 additions & 0 deletions 54_KthNodeInBST/KthNodeInBST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// 面试题54:二叉搜索树的第k个结点
// 题目:给定一棵二叉搜索树,请找出其中的第k大的结点。

#include <cstdio>
#include "../Utilities/BinaryTree.h"

const BinaryTreeNode* KthNodeCore(const BinaryTreeNode* pRoot, unsigned int& k);

const BinaryTreeNode* KthNode(const BinaryTreeNode* pRoot, unsigned int k)
{
if(pRoot == nullptr || k == 0)
return nullptr;

return KthNodeCore(pRoot, k);
}

const BinaryTreeNode* KthNodeCore(const BinaryTreeNode* pRoot, unsigned int& k)
{
const BinaryTreeNode* target = nullptr;

if(pRoot->m_pLeft != nullptr)
target = KthNodeCore(pRoot->m_pLeft, k);

if(target == nullptr)
{
if(k == 1)
target = pRoot;

k--;
}

if(target == nullptr && pRoot->m_pRight != nullptr)
target = KthNodeCore(pRoot->m_pRight, k);

return target;
}

// ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, unsigned int k, bool isNull, int expected)
{
if(testName != nullptr)
printf("%s begins: ", testName);

const BinaryTreeNode* pTarget = KthNode(pRoot, k);
if((isNull && pTarget == nullptr) || (!isNull && pTarget->m_nValue == expected))
printf("Passed.\n");
else
printf("FAILED.\n");
}

// 8
// 6 10
// 5 7 9 11
void TestA()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11);

Test("TestA0", pNode8, 0, true, -1);
Test("TestA1", pNode8, 1, false, 5);
Test("TestA2", pNode8, 2, false, 6);
Test("TestA3", pNode8, 3, false, 7);
Test("TestA4", pNode8, 4, false, 8);
Test("TestA5", pNode8, 5, false, 9);
Test("TestA6", pNode8, 6, false, 10);
Test("TestA7", pNode8, 7, false, 11);
Test("TestA8", pNode8, 8, true, -1);

DestroyTree(pNode8);

printf("\n\n");
}

// 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
void TestB()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

ConnectTreeNodes(pNode5, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode2, nullptr);
ConnectTreeNodes(pNode2, pNode1, nullptr);

Test("TestB0", pNode5, 0, true, -1);
Test("TestB1", pNode5, 1, false, 1);
Test("TestB2", pNode5, 2, false, 2);
Test("TestB3", pNode5, 3, false, 3);
Test("TestB4", pNode5, 4, false, 4);
Test("TestB5", pNode5, 5, false, 5);
Test("TestB6", pNode5, 6, true, -1);

DestroyTree(pNode5);

printf("\n\n");
}

// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
void TestC()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNode1, nullptr, pNode2);
ConnectTreeNodes(pNode2, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode5);

Test("TestC0", pNode1, 0, true, -1);
Test("TestC1", pNode1, 1, false, 1);
Test("TestC2", pNode1, 2, false, 2);
Test("TestC3", pNode1, 3, false, 3);
Test("TestC4", pNode1, 4, false, 4);
Test("TestC5", pNode1, 5, false, 5);
Test("TestC6", pNode1, 6, true, -1);

DestroyTree(pNode1);

printf("\n\n");
}

// There is only one node in a tree
void TestD()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

Test("TestD0", pNode1, 0, true, -1);
Test("TestD1", pNode1, 1, false, 1);
Test("TestD2", pNode1, 2, true, -1);

DestroyTree(pNode1);

printf("\n\n");
}

// empty tree
void TestE()
{
Test("TestE0", nullptr, 0, true, -1);
Test("TestE1", nullptr, 1, true, -1);

printf("\n\n");
}

int main(int argc, char* argv[])
{
TestA();
TestB();
TestC();
TestD();
TestE();
}
12 changes: 12 additions & 0 deletions CodingInterviewChinese2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "53_02_MissingNumber", "53_0
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "53_03_IntegerIdenticalToIndex", "53_03_IntegerIdenticalToIndex\53_03_IntegerIdenticalToIndex.vcxproj", "{072FA154-EEE1-4477-BB18-08443767ABE8}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "54_KthNodeInBST", "54_KthNodeInBST\54_KthNodeInBST.vcxproj", "{050F6A72-5004-42F6-8E1A-BA825C6C845B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -750,6 +752,16 @@ Global
{072FA154-EEE1-4477-BB18-08443767ABE8}.Release|x64.Build.0 = Release|x64
{072FA154-EEE1-4477-BB18-08443767ABE8}.Release|x86.ActiveCfg = Release|Win32
{072FA154-EEE1-4477-BB18-08443767ABE8}.Release|x86.Build.0 = Release|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Debug|Any CPU.ActiveCfg = Debug|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Debug|x64.ActiveCfg = Debug|x64
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Debug|x64.Build.0 = Debug|x64
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Debug|x86.ActiveCfg = Debug|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Debug|x86.Build.0 = Debug|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Release|Any CPU.ActiveCfg = Release|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Release|x64.ActiveCfg = Release|x64
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Release|x64.Build.0 = Release|x64
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Release|x86.ActiveCfg = Release|Win32
{050F6A72-5004-42F6-8E1A-BA825C6C845B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit c23f6a9

Please sign in to comment.