├── ArrayConstruction ├── ArrayConstruction.cpp ├── ArrayConstruction.vcxproj └── ArrayConstruction.vcxproj.filters ├── ChineseCodingInterviewAppendix.sln ├── DeleteDuplicatedListNode ├── DeleteDuplicatedListNode.vcxproj ├── DeleteDuplicatedListNode.vcxproj.filters └── DeleteDuplication.cpp ├── DuplicationInArray ├── DuplicationInArray.vcxproj ├── DuplicationInArray.vcxproj.filters └── FindDuplication.cpp ├── EntryNodeInListLoop ├── EntryNodeInListLoop.cpp ├── EntryNodeInListLoop.vcxproj └── EntryNodeInListLoop.vcxproj.filters ├── FirstCharacterInStream ├── FirstCharacterInStream.cpp ├── FirstCharacterInStream.vcxproj └── FirstCharacterInStream.vcxproj.filters ├── KthNodeInBST ├── KthNodeInBST.cpp ├── KthNodeInBST.vcxproj └── KthNodeInBST.vcxproj.filters ├── MaxInSlidingWindow ├── MaxInSlidingWindow.cpp ├── MaxInSlidingWindow.vcxproj └── MaxInSlidingWindow.vcxproj.filters ├── NextNodeInBinaryTrees ├── NextNode.cpp ├── NextNodeInBinaryTrees.vcxproj └── NextNodeInBinaryTrees.vcxproj.filters ├── NumericStrings ├── NumericString.cpp ├── NumericStrings.vcxproj └── NumericStrings.vcxproj.filters ├── PrintTreesInLines ├── PrintTreesInLines.cpp ├── PrintTreesInLines.vcxproj └── PrintTreesInLines.vcxproj.filters ├── PrintTreesInZigzag ├── PrintTreesInZigzag.vcxproj ├── PrintTreesInZigzag.vcxproj.filters └── PrintTreesInzigzag.cpp ├── README.md ├── RegularExpressionsMatching ├── RegularExpressions.cpp ├── RegularExpressionsMatching.vcxproj └── RegularExpressionsMatching.vcxproj.filters ├── RobotMove ├── RobotMove.cpp ├── RobotMove.vcxproj └── RobotMove.vcxproj.filters ├── SerializeBinaryTrees ├── SerializeBinaryTrees.cpp ├── SerializeBinaryTrees.vcxproj └── SerializeBinaryTrees.vcxproj.filters ├── StreamMedian ├── StreamMedian.cpp ├── StreamMedian.vcxproj └── StreamMedian.vcxproj.filters ├── StringPathInMatrix ├── StringPath.cpp ├── StringPathInMatrix.vcxproj └── StringPathInMatrix.vcxproj.filters ├── SymmetricalBinaryTree ├── SymmetricalBinaryTree.cpp ├── SymmetricalBinaryTree.vcxproj └── SymmetricalBinaryTree.vcxproj.filters └── Utilities ├── BinaryTree.cpp ├── BinaryTree.h ├── Utilities.vcxproj ├── Utilities.vcxproj.filters ├── Utilities.vcxproj.user ├── list.cpp └── list.h /ArrayConstruction/ArrayConstruction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhedahht/ChineseCodingInterviewAppendix/5e62cafd79bf261d7fb32dcc0e08a8d87dff8932/ArrayConstruction/ArrayConstruction.cpp -------------------------------------------------------------------------------- /ArrayConstruction/ArrayConstruction.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF} 15 | Win32Proj 16 | ArrayConstruction 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /ArrayConstruction/ArrayConstruction.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DuplicationInArray", "DuplicationInArray\DuplicationInArray.vcxproj", "{3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RegularExpressionsMatching", "RegularExpressionsMatching\RegularExpressionsMatching.vcxproj", "{3B6BF857-6BF9-42C0-9EF2-2898B7633AE7}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NumericStrings", "NumericStrings\NumericStrings.vcxproj", "{47891C37-4AB1-4332-AD03-ED1DBE8A978C}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Utilities", "Utilities\Utilities.vcxproj", "{715E6A55-16B3-4F05-9286-6DBC78DA51E4}" 11 | EndProject 12 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EntryNodeInListLoop", "EntryNodeInListLoop\EntryNodeInListLoop.vcxproj", "{C79451B8-68DD-4932-9F33-15747F317610}" 13 | ProjectSection(ProjectDependencies) = postProject 14 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4} = {715E6A55-16B3-4F05-9286-6DBC78DA51E4} 15 | EndProjectSection 16 | EndProject 17 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NextNodeInBinaryTrees", "NextNodeInBinaryTrees\NextNodeInBinaryTrees.vcxproj", "{8CF39E25-265A-4D79-B63C-E3F646643D10}" 18 | EndProject 19 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StringPathInMatrix", "StringPathInMatrix\StringPathInMatrix.vcxproj", "{1D4BD3F4-AC32-4B45-B6A5-927EE8757E89}" 20 | EndProject 21 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RobotMove", "RobotMove\RobotMove.vcxproj", "{23720702-0117-4D66-9E67-2A609E28C083}" 22 | EndProject 23 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DeleteDuplicatedListNode", "DeleteDuplicatedListNode\DeleteDuplicatedListNode.vcxproj", "{1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB}" 24 | EndProject 25 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SymmetricalBinaryTree", "SymmetricalBinaryTree\SymmetricalBinaryTree.vcxproj", "{BCEB5FAA-BDD6-4B80-8D29-34418F900EAB}" 26 | EndProject 27 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PrintTreesInLines", "PrintTreesInLines\PrintTreesInLines.vcxproj", "{B1DB40C3-7E7D-4FFB-AEE3-921E39118606}" 28 | EndProject 29 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PrintTreesInZigzag", "PrintTreesInZigzag\PrintTreesInZigzag.vcxproj", "{928E5522-02BB-493A-9F1A-EC25F285F833}" 30 | EndProject 31 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SerializeBinaryTrees", "SerializeBinaryTrees\SerializeBinaryTrees.vcxproj", "{B9DB5738-C69C-4B80-9070-FF36B1E6F772}" 32 | EndProject 33 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StreamMedian", "StreamMedian\StreamMedian.vcxproj", "{F85D00AB-0A01-4BB7-9BD9-E7786A9517CA}" 34 | EndProject 35 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FirstCharacterInStream", "FirstCharacterInStream\FirstCharacterInStream.vcxproj", "{9E019D4C-5994-40CB-8052-913877A45369}" 36 | EndProject 37 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "KthNodeInBST", "KthNodeInBST\KthNodeInBST.vcxproj", "{0DE35C6F-B24D-4F43-9E4B-EE75EDB10953}" 38 | EndProject 39 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MaxInSlidingWindow", "MaxInSlidingWindow\MaxInSlidingWindow.vcxproj", "{0720198D-FE39-49EF-9560-2E8FA9D7F570}" 40 | EndProject 41 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ArrayConstruction", "ArrayConstruction\ArrayConstruction.vcxproj", "{DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}" 42 | EndProject 43 | Global 44 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 45 | Debug|Win32 = Debug|Win32 46 | Release|Win32 = Release|Win32 47 | EndGlobalSection 48 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 49 | {3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2}.Debug|Win32.ActiveCfg = Debug|Win32 50 | {3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2}.Debug|Win32.Build.0 = Debug|Win32 51 | {3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2}.Release|Win32.ActiveCfg = Release|Win32 52 | {3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2}.Release|Win32.Build.0 = Release|Win32 53 | {3B6BF857-6BF9-42C0-9EF2-2898B7633AE7}.Debug|Win32.ActiveCfg = Debug|Win32 54 | {3B6BF857-6BF9-42C0-9EF2-2898B7633AE7}.Debug|Win32.Build.0 = Debug|Win32 55 | {3B6BF857-6BF9-42C0-9EF2-2898B7633AE7}.Release|Win32.ActiveCfg = Release|Win32 56 | {3B6BF857-6BF9-42C0-9EF2-2898B7633AE7}.Release|Win32.Build.0 = Release|Win32 57 | {47891C37-4AB1-4332-AD03-ED1DBE8A978C}.Debug|Win32.ActiveCfg = Debug|Win32 58 | {47891C37-4AB1-4332-AD03-ED1DBE8A978C}.Debug|Win32.Build.0 = Debug|Win32 59 | {47891C37-4AB1-4332-AD03-ED1DBE8A978C}.Release|Win32.ActiveCfg = Release|Win32 60 | {47891C37-4AB1-4332-AD03-ED1DBE8A978C}.Release|Win32.Build.0 = Release|Win32 61 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4}.Debug|Win32.ActiveCfg = Debug|Win32 62 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4}.Debug|Win32.Build.0 = Debug|Win32 63 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4}.Release|Win32.ActiveCfg = Release|Win32 64 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4}.Release|Win32.Build.0 = Release|Win32 65 | {C79451B8-68DD-4932-9F33-15747F317610}.Debug|Win32.ActiveCfg = Debug|Win32 66 | {C79451B8-68DD-4932-9F33-15747F317610}.Debug|Win32.Build.0 = Debug|Win32 67 | {C79451B8-68DD-4932-9F33-15747F317610}.Release|Win32.ActiveCfg = Release|Win32 68 | {C79451B8-68DD-4932-9F33-15747F317610}.Release|Win32.Build.0 = Release|Win32 69 | {8CF39E25-265A-4D79-B63C-E3F646643D10}.Debug|Win32.ActiveCfg = Debug|Win32 70 | {8CF39E25-265A-4D79-B63C-E3F646643D10}.Debug|Win32.Build.0 = Debug|Win32 71 | {8CF39E25-265A-4D79-B63C-E3F646643D10}.Release|Win32.ActiveCfg = Release|Win32 72 | {8CF39E25-265A-4D79-B63C-E3F646643D10}.Release|Win32.Build.0 = Release|Win32 73 | {1D4BD3F4-AC32-4B45-B6A5-927EE8757E89}.Debug|Win32.ActiveCfg = Debug|Win32 74 | {1D4BD3F4-AC32-4B45-B6A5-927EE8757E89}.Debug|Win32.Build.0 = Debug|Win32 75 | {1D4BD3F4-AC32-4B45-B6A5-927EE8757E89}.Release|Win32.ActiveCfg = Release|Win32 76 | {1D4BD3F4-AC32-4B45-B6A5-927EE8757E89}.Release|Win32.Build.0 = Release|Win32 77 | {23720702-0117-4D66-9E67-2A609E28C083}.Debug|Win32.ActiveCfg = Debug|Win32 78 | {23720702-0117-4D66-9E67-2A609E28C083}.Debug|Win32.Build.0 = Debug|Win32 79 | {23720702-0117-4D66-9E67-2A609E28C083}.Release|Win32.ActiveCfg = Release|Win32 80 | {23720702-0117-4D66-9E67-2A609E28C083}.Release|Win32.Build.0 = Release|Win32 81 | {1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB}.Debug|Win32.ActiveCfg = Debug|Win32 82 | {1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB}.Debug|Win32.Build.0 = Debug|Win32 83 | {1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB}.Release|Win32.ActiveCfg = Release|Win32 84 | {1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB}.Release|Win32.Build.0 = Release|Win32 85 | {BCEB5FAA-BDD6-4B80-8D29-34418F900EAB}.Debug|Win32.ActiveCfg = Debug|Win32 86 | {BCEB5FAA-BDD6-4B80-8D29-34418F900EAB}.Debug|Win32.Build.0 = Debug|Win32 87 | {BCEB5FAA-BDD6-4B80-8D29-34418F900EAB}.Release|Win32.ActiveCfg = Release|Win32 88 | {BCEB5FAA-BDD6-4B80-8D29-34418F900EAB}.Release|Win32.Build.0 = Release|Win32 89 | {B1DB40C3-7E7D-4FFB-AEE3-921E39118606}.Debug|Win32.ActiveCfg = Debug|Win32 90 | {B1DB40C3-7E7D-4FFB-AEE3-921E39118606}.Debug|Win32.Build.0 = Debug|Win32 91 | {B1DB40C3-7E7D-4FFB-AEE3-921E39118606}.Release|Win32.ActiveCfg = Release|Win32 92 | {B1DB40C3-7E7D-4FFB-AEE3-921E39118606}.Release|Win32.Build.0 = Release|Win32 93 | {928E5522-02BB-493A-9F1A-EC25F285F833}.Debug|Win32.ActiveCfg = Debug|Win32 94 | {928E5522-02BB-493A-9F1A-EC25F285F833}.Debug|Win32.Build.0 = Debug|Win32 95 | {928E5522-02BB-493A-9F1A-EC25F285F833}.Release|Win32.ActiveCfg = Release|Win32 96 | {928E5522-02BB-493A-9F1A-EC25F285F833}.Release|Win32.Build.0 = Release|Win32 97 | {B9DB5738-C69C-4B80-9070-FF36B1E6F772}.Debug|Win32.ActiveCfg = Debug|Win32 98 | {B9DB5738-C69C-4B80-9070-FF36B1E6F772}.Debug|Win32.Build.0 = Debug|Win32 99 | {B9DB5738-C69C-4B80-9070-FF36B1E6F772}.Release|Win32.ActiveCfg = Release|Win32 100 | {B9DB5738-C69C-4B80-9070-FF36B1E6F772}.Release|Win32.Build.0 = Release|Win32 101 | {F85D00AB-0A01-4BB7-9BD9-E7786A9517CA}.Debug|Win32.ActiveCfg = Debug|Win32 102 | {F85D00AB-0A01-4BB7-9BD9-E7786A9517CA}.Debug|Win32.Build.0 = Debug|Win32 103 | {F85D00AB-0A01-4BB7-9BD9-E7786A9517CA}.Release|Win32.ActiveCfg = Release|Win32 104 | {F85D00AB-0A01-4BB7-9BD9-E7786A9517CA}.Release|Win32.Build.0 = Release|Win32 105 | {9E019D4C-5994-40CB-8052-913877A45369}.Debug|Win32.ActiveCfg = Debug|Win32 106 | {9E019D4C-5994-40CB-8052-913877A45369}.Debug|Win32.Build.0 = Debug|Win32 107 | {9E019D4C-5994-40CB-8052-913877A45369}.Release|Win32.ActiveCfg = Release|Win32 108 | {9E019D4C-5994-40CB-8052-913877A45369}.Release|Win32.Build.0 = Release|Win32 109 | {0DE35C6F-B24D-4F43-9E4B-EE75EDB10953}.Debug|Win32.ActiveCfg = Debug|Win32 110 | {0DE35C6F-B24D-4F43-9E4B-EE75EDB10953}.Debug|Win32.Build.0 = Debug|Win32 111 | {0DE35C6F-B24D-4F43-9E4B-EE75EDB10953}.Release|Win32.ActiveCfg = Release|Win32 112 | {0DE35C6F-B24D-4F43-9E4B-EE75EDB10953}.Release|Win32.Build.0 = Release|Win32 113 | {0720198D-FE39-49EF-9560-2E8FA9D7F570}.Debug|Win32.ActiveCfg = Debug|Win32 114 | {0720198D-FE39-49EF-9560-2E8FA9D7F570}.Debug|Win32.Build.0 = Debug|Win32 115 | {0720198D-FE39-49EF-9560-2E8FA9D7F570}.Release|Win32.ActiveCfg = Release|Win32 116 | {0720198D-FE39-49EF-9560-2E8FA9D7F570}.Release|Win32.Build.0 = Release|Win32 117 | {DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}.Debug|Win32.ActiveCfg = Debug|Win32 118 | {DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}.Debug|Win32.Build.0 = Debug|Win32 119 | {DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}.Release|Win32.ActiveCfg = Release|Win32 120 | {DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}.Release|Win32.Build.0 = Release|Win32 121 | EndGlobalSection 122 | GlobalSection(SolutionProperties) = preSolution 123 | HideSolutionNode = FALSE 124 | EndGlobalSection 125 | EndGlobal 126 | -------------------------------------------------------------------------------- /DeleteDuplicatedListNode/DeleteDuplicatedListNode.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {1B65A9BE-BEC6-48D1-A2DD-CEA2A83D63DB} 15 | Win32Proj 16 | DeleteDuplicatedListNode 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /DeleteDuplicatedListNode/DeleteDuplicatedListNode.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /DeleteDuplicatedListNode/DeleteDuplication.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 44 in ) Given a sorted linked list, please delete all duplicated numbers and 4 | * leave only distinct numbers from the original list. 5 | */ 6 | 7 | #include 8 | #include "../Utilities/list.h" 9 | 10 | void deleteDuplication(ListNode** pHead) 11 | { 12 | if(pHead == NULL || *pHead == NULL) 13 | return; 14 | 15 | ListNode* pPreNode = NULL; 16 | ListNode* pNode = *pHead; 17 | while(pNode != NULL) 18 | { 19 | ListNode *pNext = pNode->m_pNext; 20 | bool needDelete = false; 21 | if(pNext != NULL && pNext->m_nValue == pNode->m_nValue) 22 | needDelete = true; 23 | 24 | if(!needDelete) 25 | { 26 | pPreNode = pNode; 27 | pNode = pNode->m_pNext; 28 | } 29 | else 30 | { 31 | int value = pNode->m_nValue; 32 | ListNode* pToBeDel = pNode; 33 | while(pToBeDel != NULL && pToBeDel->m_nValue == value) 34 | { 35 | pNext = pToBeDel->m_pNext; 36 | 37 | delete pToBeDel; 38 | pToBeDel = NULL; 39 | 40 | pToBeDel = pNext; 41 | } 42 | 43 | if(pPreNode == NULL) 44 | *pHead = pNext; 45 | else 46 | pPreNode->m_pNext = pNext; 47 | pNode = pNext; 48 | } 49 | } 50 | } 51 | 52 | // ==================== Test Code ==================== 53 | void Test(char* testName, ListNode** pHead, int* expectedValues, int expectedLength) 54 | { 55 | if(testName != NULL) 56 | printf("%s begins: ", testName); 57 | 58 | deleteDuplication(pHead); 59 | 60 | int index = 0; 61 | ListNode* pNode = *pHead; 62 | while(pNode != NULL && index < expectedLength) 63 | { 64 | if(pNode->m_nValue != expectedValues[index]) 65 | break; 66 | 67 | pNode = pNode->m_pNext; 68 | index++; 69 | } 70 | 71 | if(pNode == NULL && index == expectedLength) 72 | printf("Passed.\n"); 73 | else 74 | printf("FAILED.\n"); 75 | } 76 | 77 | // some nodes are duplicated 78 | void Test1() 79 | { 80 | ListNode* pNode1 = CreateListNode(1); 81 | ListNode* pNode2 = CreateListNode(2); 82 | ListNode* pNode3 = CreateListNode(3); 83 | ListNode* pNode4 = CreateListNode(3); 84 | ListNode* pNode5 = CreateListNode(4); 85 | ListNode* pNode6 = CreateListNode(4); 86 | ListNode* pNode7 = CreateListNode(5); 87 | 88 | ConnectListNodes(pNode1, pNode2); 89 | ConnectListNodes(pNode2, pNode3); 90 | ConnectListNodes(pNode3, pNode4); 91 | ConnectListNodes(pNode4, pNode5); 92 | ConnectListNodes(pNode5, pNode6); 93 | ConnectListNodes(pNode6, pNode7); 94 | 95 | ListNode* pHead = pNode1; 96 | 97 | int expectedValues[] = {1, 2, 5}; 98 | Test("Test1", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 99 | 100 | DestroyList(pHead); 101 | } 102 | 103 | // all nodes are unique 104 | void Test2() 105 | { 106 | ListNode* pNode1 = CreateListNode(1); 107 | ListNode* pNode2 = CreateListNode(2); 108 | ListNode* pNode3 = CreateListNode(3); 109 | ListNode* pNode4 = CreateListNode(4); 110 | ListNode* pNode5 = CreateListNode(5); 111 | ListNode* pNode6 = CreateListNode(6); 112 | ListNode* pNode7 = CreateListNode(7); 113 | 114 | ConnectListNodes(pNode1, pNode2); 115 | ConnectListNodes(pNode2, pNode3); 116 | ConnectListNodes(pNode3, pNode4); 117 | ConnectListNodes(pNode4, pNode5); 118 | ConnectListNodes(pNode5, pNode6); 119 | ConnectListNodes(pNode6, pNode7); 120 | 121 | ListNode* pHead = pNode1; 122 | 123 | int expectedValues[] = {1, 2, 3, 4, 5, 6, 7}; 124 | Test("Test2", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 125 | 126 | DestroyList(pHead); 127 | } 128 | 129 | // all nodes are duplicated except one 130 | void Test3() 131 | { 132 | ListNode* pNode1 = CreateListNode(1); 133 | ListNode* pNode2 = CreateListNode(1); 134 | ListNode* pNode3 = CreateListNode(1); 135 | ListNode* pNode4 = CreateListNode(1); 136 | ListNode* pNode5 = CreateListNode(1); 137 | ListNode* pNode6 = CreateListNode(1); 138 | ListNode* pNode7 = CreateListNode(2); 139 | 140 | ConnectListNodes(pNode1, pNode2); 141 | ConnectListNodes(pNode2, pNode3); 142 | ConnectListNodes(pNode3, pNode4); 143 | ConnectListNodes(pNode4, pNode5); 144 | ConnectListNodes(pNode5, pNode6); 145 | ConnectListNodes(pNode6, pNode7); 146 | 147 | ListNode* pHead = pNode1; 148 | 149 | int expectedValues[] = {2}; 150 | Test("Test3", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 151 | 152 | DestroyList(pHead); 153 | } 154 | 155 | // all nodes are duplicated 156 | void Test4() 157 | { 158 | ListNode* pNode1 = CreateListNode(1); 159 | ListNode* pNode2 = CreateListNode(1); 160 | ListNode* pNode3 = CreateListNode(1); 161 | ListNode* pNode4 = CreateListNode(1); 162 | ListNode* pNode5 = CreateListNode(1); 163 | ListNode* pNode6 = CreateListNode(1); 164 | ListNode* pNode7 = CreateListNode(1); 165 | 166 | ConnectListNodes(pNode1, pNode2); 167 | ConnectListNodes(pNode2, pNode3); 168 | ConnectListNodes(pNode3, pNode4); 169 | ConnectListNodes(pNode4, pNode5); 170 | ConnectListNodes(pNode5, pNode6); 171 | ConnectListNodes(pNode6, pNode7); 172 | 173 | ListNode* pHead = pNode1; 174 | 175 | Test("Test4", &pHead, NULL, 0); 176 | 177 | DestroyList(pHead); 178 | } 179 | 180 | // all nodes are duplicated in pairs 181 | void Test5() 182 | { 183 | ListNode* pNode1 = CreateListNode(1); 184 | ListNode* pNode2 = CreateListNode(1); 185 | ListNode* pNode3 = CreateListNode(2); 186 | ListNode* pNode4 = CreateListNode(2); 187 | ListNode* pNode5 = CreateListNode(3); 188 | ListNode* pNode6 = CreateListNode(3); 189 | ListNode* pNode7 = CreateListNode(4); 190 | ListNode* pNode8 = CreateListNode(4); 191 | 192 | ConnectListNodes(pNode1, pNode2); 193 | ConnectListNodes(pNode2, pNode3); 194 | ConnectListNodes(pNode3, pNode4); 195 | ConnectListNodes(pNode4, pNode5); 196 | ConnectListNodes(pNode5, pNode6); 197 | ConnectListNodes(pNode6, pNode7); 198 | ConnectListNodes(pNode7, pNode8); 199 | 200 | ListNode* pHead = pNode1; 201 | 202 | Test("Test5", &pHead, NULL, 0); 203 | 204 | DestroyList(pHead); 205 | } 206 | 207 | // nodes are duplicated in pairs except two 208 | void Test6() 209 | { 210 | ListNode* pNode1 = CreateListNode(1); 211 | ListNode* pNode2 = CreateListNode(1); 212 | ListNode* pNode3 = CreateListNode(2); 213 | ListNode* pNode4 = CreateListNode(3); 214 | ListNode* pNode5 = CreateListNode(3); 215 | ListNode* pNode6 = CreateListNode(4); 216 | ListNode* pNode7 = CreateListNode(5); 217 | ListNode* pNode8 = CreateListNode(5); 218 | 219 | ConnectListNodes(pNode1, pNode2); 220 | ConnectListNodes(pNode2, pNode3); 221 | ConnectListNodes(pNode3, pNode4); 222 | ConnectListNodes(pNode4, pNode5); 223 | ConnectListNodes(pNode5, pNode6); 224 | ConnectListNodes(pNode6, pNode7); 225 | ConnectListNodes(pNode7, pNode8); 226 | 227 | ListNode* pHead = pNode1; 228 | 229 | int expectedValues[] = {2, 4}; 230 | Test("Test6", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 231 | 232 | DestroyList(pHead); 233 | } 234 | 235 | // a list with two unique nodes 236 | void Test7() 237 | { 238 | ListNode* pNode1 = CreateListNode(1); 239 | ListNode* pNode2 = CreateListNode(2); 240 | 241 | ConnectListNodes(pNode1, pNode2); 242 | 243 | ListNode* pHead = pNode1; 244 | 245 | int expectedValues[] = {1, 2}; 246 | Test("Test7", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 247 | 248 | DestroyList(pHead); 249 | } 250 | 251 | // only one node in a list 252 | void Test8() 253 | { 254 | ListNode* pNode1 = CreateListNode(1); 255 | 256 | ConnectListNodes(pNode1, NULL); 257 | 258 | ListNode* pHead = pNode1; 259 | 260 | int expectedValues[] = {1}; 261 | Test("Test8", &pHead, expectedValues, sizeof(expectedValues)/sizeof(int)); 262 | 263 | DestroyList(pHead); 264 | } 265 | 266 | // a list with only two duplidated nodes 267 | void Test9() 268 | { 269 | ListNode* pNode1 = CreateListNode(1); 270 | ListNode* pNode2 = CreateListNode(1); 271 | 272 | ConnectListNodes(pNode1, pNode2); 273 | 274 | ListNode* pHead = pNode1; 275 | 276 | Test("Test9", &pHead, NULL, 0); 277 | 278 | DestroyList(pHead); 279 | } 280 | 281 | // empty list 282 | void Test10() 283 | { 284 | ListNode* pHead = NULL; 285 | 286 | Test("Test10", &pHead, NULL, 0); 287 | } 288 | 289 | int main(int argc, char* argv[]) 290 | { 291 | Test1(); 292 | Test2(); 293 | Test3(); 294 | Test4(); 295 | Test5(); 296 | Test6(); 297 | Test7(); 298 | Test8(); 299 | Test9(); 300 | Test10(); 301 | 302 | return 0; 303 | } -------------------------------------------------------------------------------- /DuplicationInArray/DuplicationInArray.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {3DD5DB6F-68FB-4999-94D5-F4637AB8D8C2} 15 | Win32Proj 16 | DuplicationInArray 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /DuplicationInArray/DuplicationInArray.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /DuplicationInArray/FindDuplication.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 6 in ) An array contains n numbers ranging from 0 to n-1. 4 | * There are some numbers duplicated in the array. It is not clear how many numbers are duplicated or 5 | * how many times a number gets duplicated. How do you find a duplicated number in the array? 6 | * For example, if an array of length 7 contains the numbers {2, 3, 1, 0, 2, 5, 3}, 7 | * the implemented function (or method) should return either 2 or 3 8 | */ 9 | 10 | #include 11 | 12 | // Parameters: 13 | // numbers: an array of integers 14 | // length: the length of array numbers 15 | // duplication: (Output) the duplicated number in the array number 16 | // Return value: true if the input is valid, and there are some duplications in the array number 17 | // otherwise false 18 | bool duplicate(int numbers[], int length, int* duplication) 19 | { 20 | if(numbers == NULL || length <= 0) 21 | { 22 | return false; 23 | } 24 | 25 | for(int i = 0; i < length; ++i) 26 | { 27 | if(numbers[i] < 0 || numbers[i] > length - 1) 28 | return false; 29 | } 30 | 31 | for(int i = 0; i < length; ++i) 32 | { 33 | while(numbers[i] != i) 34 | { 35 | if(numbers[i] == numbers[numbers[i]]) 36 | { 37 | *duplication = numbers[i]; 38 | return true; 39 | } 40 | 41 | // swap numbers[i] and numbers[numbers[i]] 42 | int temp = numbers[i]; 43 | numbers[i] = numbers[temp]; 44 | numbers[temp] = temp; 45 | } 46 | } 47 | 48 | return false; 49 | } 50 | 51 | //================= Test Code ================= 52 | bool contains(int array[], int length, int number) 53 | { 54 | for(int i = 0; i < length; ++i) 55 | { 56 | if(array[i] == number) 57 | return true; 58 | } 59 | 60 | return false; 61 | } 62 | 63 | void test(char* testName, int numbers[], int lengthNumbers, int expected[], int expectedExpected, bool validArgument) 64 | { 65 | printf("%s begins: ", testName); 66 | 67 | int duplication; 68 | bool validInput = duplicate(numbers, lengthNumbers, &duplication); 69 | 70 | if(validArgument == validInput) 71 | { 72 | if(validArgument) 73 | { 74 | if(contains(expected, expectedExpected, duplication)) 75 | { 76 | printf("Passed.\n"); 77 | } 78 | else 79 | { 80 | printf("FAILED.\n"); 81 | } 82 | } 83 | else 84 | { 85 | printf("Passed.\n"); 86 | } 87 | } 88 | else 89 | { 90 | printf("FAILED.\n"); 91 | } 92 | } 93 | 94 | // The duplicated number is the smallest number 95 | static void test1() 96 | { 97 | int numbers[] = {2, 1, 3, 1, 4}; 98 | int duplications[] = {1}; 99 | test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); 100 | } 101 | 102 | // The duplicated number is the greatest number 103 | static void test2() 104 | { 105 | int numbers[] = {2, 4, 3, 1, 4}; 106 | int duplications[] = {4}; 107 | test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); 108 | } 109 | 110 | // There are more than one duplicated number 111 | static void test3() 112 | { 113 | int numbers[] = {2, 4, 2, 1, 4}; 114 | int duplications[] = {2, 4}; 115 | test("Test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true); 116 | } 117 | 118 | // no duplicated numbers 119 | static void test4() 120 | { 121 | int numbers[] = {2, 1, 3, 0, 4}; 122 | int duplications[] = {-1}; // not in use in the test function 123 | test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false); 124 | } 125 | 126 | // no duplicated numbers 127 | static void test5() 128 | { 129 | int numbers[] = {2, 1, 3, 5, 4}; 130 | int duplications[] = {-1}; // not in use in the test function 131 | test("Test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false); 132 | } 133 | 134 | void main() 135 | { 136 | test1(); 137 | test2(); 138 | test3(); 139 | test4(); 140 | test5(); 141 | } 142 | 143 | -------------------------------------------------------------------------------- /EntryNodeInListLoop/EntryNodeInListLoop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 17 in ) If there is a loop in a linked list, how do you get the entry node of the loop? 4 | * The entry node is the first node in the loop from the head of a list. 5 | */ 6 | 7 | #include 8 | #include "../Utilities/list.h" 9 | 10 | ListNode* MeetingNode(ListNode* pHead) 11 | { 12 | if(pHead == NULL) 13 | return NULL; 14 | 15 | ListNode* pSlow = pHead->m_pNext; 16 | if(pSlow == NULL) 17 | return NULL; 18 | 19 | ListNode* pFast = pSlow->m_pNext; 20 | while(pFast != NULL && pSlow != NULL) 21 | { 22 | if(pFast == pSlow) 23 | return pFast; 24 | 25 | pSlow = pSlow->m_pNext; 26 | 27 | pFast = pFast->m_pNext; 28 | if(pFast != NULL) 29 | pFast = pFast->m_pNext; 30 | } 31 | 32 | return NULL; 33 | } 34 | 35 | ListNode* EntryNodeOfLoop(ListNode* pHead) 36 | { 37 | ListNode* meetingNode = MeetingNode(pHead); 38 | if(meetingNode == NULL) 39 | return NULL; 40 | 41 | // get the number of nodes in loop 42 | int nodesInLoop = 1; 43 | ListNode* pNode1 = meetingNode; 44 | while(pNode1->m_pNext != meetingNode) 45 | { 46 | pNode1 = pNode1->m_pNext; 47 | ++nodesInLoop; 48 | } 49 | 50 | // move pNode1 51 | pNode1 = pHead; 52 | for(int i = 0; i < nodesInLoop; ++i) 53 | pNode1 = pNode1->m_pNext; 54 | 55 | // move pNode1 and pNode2 56 | ListNode* pNode2 = pHead; 57 | while(pNode1 != pNode2) 58 | { 59 | pNode1 = pNode1->m_pNext; 60 | pNode2 = pNode2->m_pNext; 61 | } 62 | 63 | return pNode1; 64 | } 65 | 66 | // ==================== Test Code ==================== 67 | void Test(char* testName, ListNode* pHead, ListNode* entryNode) 68 | { 69 | if(testName != NULL) 70 | printf("%s begins: ", testName); 71 | 72 | if(EntryNodeOfLoop(pHead) == entryNode) 73 | printf("Passed.\n"); 74 | else 75 | printf("FAILED.\n"); 76 | } 77 | 78 | // A list has a node, without a loop 79 | void Test1() 80 | { 81 | ListNode* pNode1 = CreateListNode(1); 82 | 83 | Test("Test1", pNode1, NULL); 84 | 85 | DestroyList(pNode1); 86 | } 87 | 88 | // A list has a node, with a loop 89 | void Test2() 90 | { 91 | ListNode* pNode1 = CreateListNode(1); 92 | ConnectListNodes(pNode1, pNode1); 93 | 94 | Test("Test2", pNode1, pNode1); 95 | 96 | delete pNode1; 97 | pNode1 = NULL; 98 | } 99 | 100 | // A list has multiple nodes, with a loop 101 | void Test3() 102 | { 103 | ListNode* pNode1 = CreateListNode(1); 104 | ListNode* pNode2 = CreateListNode(2); 105 | ListNode* pNode3 = CreateListNode(3); 106 | ListNode* pNode4 = CreateListNode(4); 107 | ListNode* pNode5 = CreateListNode(5); 108 | 109 | ConnectListNodes(pNode1, pNode2); 110 | ConnectListNodes(pNode2, pNode3); 111 | ConnectListNodes(pNode3, pNode4); 112 | ConnectListNodes(pNode4, pNode5); 113 | ConnectListNodes(pNode5, pNode3); 114 | 115 | Test("Test3", pNode1, pNode3); 116 | 117 | delete pNode1; 118 | pNode1 = NULL; 119 | delete pNode2; 120 | pNode2 = NULL; 121 | delete pNode3; 122 | pNode3 = NULL; 123 | delete pNode4; 124 | pNode4 = NULL; 125 | delete pNode5; 126 | pNode5 = NULL; 127 | } 128 | 129 | // A list has multiple nodes, with a loop 130 | void Test4() 131 | { 132 | ListNode* pNode1 = CreateListNode(1); 133 | ListNode* pNode2 = CreateListNode(2); 134 | ListNode* pNode3 = CreateListNode(3); 135 | ListNode* pNode4 = CreateListNode(4); 136 | ListNode* pNode5 = CreateListNode(5); 137 | 138 | ConnectListNodes(pNode1, pNode2); 139 | ConnectListNodes(pNode2, pNode3); 140 | ConnectListNodes(pNode3, pNode4); 141 | ConnectListNodes(pNode4, pNode5); 142 | ConnectListNodes(pNode5, pNode1); 143 | 144 | Test("Test4", pNode1, pNode1); 145 | 146 | delete pNode1; 147 | pNode1 = NULL; 148 | delete pNode2; 149 | pNode2 = NULL; 150 | delete pNode3; 151 | pNode3 = NULL; 152 | delete pNode4; 153 | pNode4 = NULL; 154 | delete pNode5; 155 | pNode5 = NULL; 156 | } 157 | 158 | // A list has multiple nodes, with a loop 159 | void Test5() 160 | { 161 | ListNode* pNode1 = CreateListNode(1); 162 | ListNode* pNode2 = CreateListNode(2); 163 | ListNode* pNode3 = CreateListNode(3); 164 | ListNode* pNode4 = CreateListNode(4); 165 | ListNode* pNode5 = CreateListNode(5); 166 | 167 | ConnectListNodes(pNode1, pNode2); 168 | ConnectListNodes(pNode2, pNode3); 169 | ConnectListNodes(pNode3, pNode4); 170 | ConnectListNodes(pNode4, pNode5); 171 | ConnectListNodes(pNode5, pNode5); 172 | 173 | Test("Test5", pNode1, pNode5); 174 | 175 | delete pNode1; 176 | pNode1 = NULL; 177 | delete pNode2; 178 | pNode2 = NULL; 179 | delete pNode3; 180 | pNode3 = NULL; 181 | delete pNode4; 182 | pNode4 = NULL; 183 | delete pNode5; 184 | pNode5 = NULL; 185 | } 186 | 187 | // A list has multiple nodes, without a loop 188 | void Test6() 189 | { 190 | ListNode* pNode1 = CreateListNode(1); 191 | ListNode* pNode2 = CreateListNode(2); 192 | ListNode* pNode3 = CreateListNode(3); 193 | ListNode* pNode4 = CreateListNode(4); 194 | ListNode* pNode5 = CreateListNode(5); 195 | 196 | ConnectListNodes(pNode1, pNode2); 197 | ConnectListNodes(pNode2, pNode3); 198 | ConnectListNodes(pNode3, pNode4); 199 | ConnectListNodes(pNode4, pNode5); 200 | 201 | Test("Test6", pNode1, NULL); 202 | 203 | DestroyList(pNode1); 204 | } 205 | 206 | // Empty list 207 | void Test7() 208 | { 209 | Test("Test7", NULL, NULL); 210 | } 211 | 212 | int main(int argc, char* argv[]) 213 | { 214 | Test1(); 215 | Test2(); 216 | Test3(); 217 | Test4(); 218 | Test5(); 219 | Test6(); 220 | Test7(); 221 | 222 | return 0; 223 | } -------------------------------------------------------------------------------- /EntryNodeInListLoop/EntryNodeInListLoop.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {C79451B8-68DD-4932-9F33-15747F317610} 15 | Win32Proj 16 | EntryNodeInListLoop 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /EntryNodeInListLoop/EntryNodeInListLoop.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /FirstCharacterInStream/FirstCharacterInStream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhedahht/ChineseCodingInterviewAppendix/5e62cafd79bf261d7fb32dcc0e08a8d87dff8932/FirstCharacterInStream/FirstCharacterInStream.cpp -------------------------------------------------------------------------------- /FirstCharacterInStream/FirstCharacterInStream.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {9E019D4C-5994-40CB-8052-913877A45369} 15 | Win32Proj 16 | FirstCharacterInStream 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /FirstCharacterInStream/FirstCharacterInStream.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /KthNodeInBST/KthNodeInBST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 84 in ) How do you get the kth node in a binary search tree in an incremental order of values? 4 | */ 5 | 6 | #include 7 | #include 8 | #include "../Utilities/BinaryTree.h" 9 | 10 | BinaryTreeNode* KthNodeCore(BinaryTreeNode* pRoot, unsigned int& k); 11 | 12 | BinaryTreeNode* KthNode(BinaryTreeNode* pRoot, unsigned int k) 13 | { 14 | if(pRoot == NULL || k == 0) 15 | return NULL; 16 | 17 | return KthNodeCore(pRoot, k); 18 | } 19 | 20 | BinaryTreeNode* KthNodeCore(BinaryTreeNode* pRoot, unsigned int& k) 21 | { 22 | BinaryTreeNode* target = NULL; 23 | 24 | if(pRoot->m_pLeft != NULL) 25 | target = KthNodeCore(pRoot->m_pLeft, k); 26 | 27 | if(target == NULL) 28 | { 29 | if(k == 1) 30 | target = pRoot; 31 | 32 | k--; 33 | } 34 | 35 | if(target == NULL && pRoot->m_pRight != NULL) 36 | target = KthNodeCore(pRoot->m_pRight, k); 37 | 38 | return target; 39 | } 40 | 41 | void Test(char* testName, BinaryTreeNode* pRoot, unsigned int k, bool isNull, int expected) 42 | { 43 | if(testName != NULL) 44 | printf("%s begins: ", testName); 45 | 46 | BinaryTreeNode* pTarget = KthNode(pRoot, k); 47 | if((isNull && pTarget == NULL) || (!isNull && pTarget->m_nValue == expected)) 48 | printf("Passed.\n"); 49 | else 50 | printf("FAILED.\n"); 51 | } 52 | 53 | // 8 54 | // 6 10 55 | // 5 7 9 11 56 | void TestA() 57 | { 58 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 59 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 60 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 61 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 62 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 63 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 64 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 65 | 66 | ConnectTreeNodes(pNode8, pNode6, pNode10); 67 | ConnectTreeNodes(pNode6, pNode5, pNode7); 68 | ConnectTreeNodes(pNode10, pNode9, pNode11); 69 | 70 | Test("TestA0", pNode8, 0, true, -1); 71 | Test("TestA1", pNode8, 1, false, 5); 72 | Test("TestA2", pNode8, 2, false, 6); 73 | Test("TestA3", pNode8, 3, false, 7); 74 | Test("TestA4", pNode8, 4, false, 8); 75 | Test("TestA5", pNode8, 5, false, 9); 76 | Test("TestA6", pNode8, 6, false, 10); 77 | Test("TestA7", pNode8, 7, false, 11); 78 | Test("TestA8", pNode8, 8, true, -1); 79 | 80 | DestroyTree(pNode8); 81 | 82 | printf("\n\n"); 83 | } 84 | 85 | // 5 86 | // / 87 | // 4 88 | // / 89 | // 3 90 | // / 91 | // 2 92 | // / 93 | // 1 94 | void TestB() 95 | { 96 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 97 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 98 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 99 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 100 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 101 | 102 | ConnectTreeNodes(pNode5, pNode4, NULL); 103 | ConnectTreeNodes(pNode4, pNode3, NULL); 104 | ConnectTreeNodes(pNode3, pNode2, NULL); 105 | ConnectTreeNodes(pNode2, pNode1, NULL); 106 | 107 | Test("TestB0", pNode5, 0, true, -1); 108 | Test("TestB1", pNode5, 1, false, 1); 109 | Test("TestB2", pNode5, 2, false, 2); 110 | Test("TestB3", pNode5, 3, false, 3); 111 | Test("TestB4", pNode5, 4, false, 4); 112 | Test("TestB5", pNode5, 5, false, 5); 113 | Test("TestB6", pNode5, 6, true, -1); 114 | 115 | DestroyTree(pNode5); 116 | 117 | printf("\n\n"); 118 | } 119 | 120 | // 1 121 | // \ 122 | // 2 123 | // \ 124 | // 3 125 | // \ 126 | // 4 127 | // \ 128 | // 5 129 | void TestC() 130 | { 131 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 132 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 133 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 134 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 135 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 136 | 137 | ConnectTreeNodes(pNode1, NULL, pNode2); 138 | ConnectTreeNodes(pNode2, NULL, pNode3); 139 | ConnectTreeNodes(pNode3, NULL, pNode4); 140 | ConnectTreeNodes(pNode4, NULL, pNode5); 141 | 142 | Test("TestC0", pNode1, 0, true, -1); 143 | Test("TestC1", pNode1, 1, false, 1); 144 | Test("TestC2", pNode1, 2, false, 2); 145 | Test("TestC3", pNode1, 3, false, 3); 146 | Test("TestC4", pNode1, 4, false, 4); 147 | Test("TestC5", pNode1, 5, false, 5); 148 | Test("TestC6", pNode1, 6, true, -1); 149 | 150 | DestroyTree(pNode1); 151 | 152 | printf("\n\n"); 153 | } 154 | 155 | // There is only one node in a tree 156 | void TestD() 157 | { 158 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 159 | 160 | Test("TestD0", pNode1, 0, true, -1); 161 | Test("TestD1", pNode1, 1, false, 1); 162 | Test("TestD2", pNode1, 2, true, -1); 163 | 164 | DestroyTree(pNode1); 165 | 166 | printf("\n\n"); 167 | } 168 | 169 | // empty tree 170 | void TestE() 171 | { 172 | Test("TestE0", NULL, 0, true, -1); 173 | Test("TestE1", NULL, 1, true, -1); 174 | 175 | printf("\n\n"); 176 | } 177 | 178 | int main(int argc, char* argv[]) 179 | { 180 | TestA(); 181 | TestB(); 182 | TestC(); 183 | TestD(); 184 | TestE(); 185 | } -------------------------------------------------------------------------------- /KthNodeInBST/KthNodeInBST.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {0DE35C6F-B24D-4F43-9E4B-EE75EDB10953} 15 | Win32Proj 16 | KthNodeInBST 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /KthNodeInBST/KthNodeInBST.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /MaxInSlidingWindow/MaxInSlidingWindow.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 93 in ) Given an array of numumbers and a sliding window size, 4 | * how do you get the maximum numbers in all sliding windows? 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | vector maxInWindows(const vector& num, unsigned int size) 14 | { 15 | vector maxInWindows; 16 | if(num.size() >= size && size >= 1) 17 | { 18 | deque index; 19 | 20 | for(unsigned int i = 0; i < size; ++i) 21 | { 22 | while(!index.empty() && num[i] >= num[index.back()]) 23 | index.pop_back(); 24 | 25 | index.push_back(i); 26 | } 27 | 28 | for(unsigned int i = size; i < num.size(); ++i) 29 | { 30 | maxInWindows.push_back(num[index.front()]); 31 | 32 | while(!index.empty() && num[i] >= num[index.back()]) 33 | index.pop_back(); 34 | if(!index.empty() && index.front() <= (int)(i - size)) 35 | index.pop_front(); 36 | 37 | index.push_back(i); 38 | } 39 | maxInWindows.push_back(num[index.front()]); 40 | } 41 | 42 | return maxInWindows; 43 | } 44 | 45 | // ==================== Test Code ==================== 46 | void Test(char* testName, const vector& num, unsigned int size, const vector& expected) 47 | { 48 | if(testName != NULL) 49 | printf("%s begins: ", testName); 50 | 51 | vector result = maxInWindows(num, size); 52 | 53 | vector::const_iterator iterResult = result.begin(); 54 | vector::const_iterator iterExpected = expected.begin(); 55 | while(iterResult < result.end() && iterExpected < expected.end()) 56 | { 57 | if(*iterResult != *iterExpected) 58 | break; 59 | 60 | ++iterResult; 61 | ++iterExpected; 62 | } 63 | 64 | if(iterResult == result.end() && iterExpected == expected.end()) 65 | printf("Passed.\n"); 66 | else 67 | printf("FAILED.\n"); 68 | } 69 | 70 | void Test1() 71 | { 72 | int num[] = {2, 3, 4, 2, 6, 2, 5, 1}; 73 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 74 | 75 | int expected[] = {4, 4, 6, 6, 6, 5}; 76 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 77 | 78 | unsigned int size = 3; 79 | 80 | Test("Test1", vecNumbers, size, vecExpected); 81 | } 82 | 83 | void Test2() 84 | { 85 | int num[] = {1, 3, -1, -3, 5, 3, 6, 7}; 86 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 87 | 88 | int expected[] = {3, 3, 5, 5, 6, 7}; 89 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 90 | 91 | unsigned int size = 3; 92 | 93 | Test("Test2", vecNumbers, size, vecExpected); 94 | } 95 | 96 | // increasingly sorted 97 | void Test3() 98 | { 99 | int num[] = {1, 3, 5, 7, 9, 11, 13, 15}; 100 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 101 | 102 | int expected[] = {7, 9, 11, 13, 15}; 103 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 104 | 105 | unsigned int size = 4; 106 | 107 | Test("Test3", vecNumbers, size, vecExpected); 108 | } 109 | 110 | // decreasingly sorted 111 | void Test4() 112 | { 113 | int num[] = {16, 14, 12, 10, 8, 6, 4}; 114 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 115 | 116 | int expected[] = {16, 14, 12}; 117 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 118 | 119 | unsigned int size = 5; 120 | 121 | Test("Test4", vecNumbers, size, vecExpected); 122 | } 123 | 124 | // size of sliding windows is 1 125 | void Test5() 126 | { 127 | int num[] = {10, 14, 12, 11}; 128 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 129 | 130 | int expected[] = {10, 14, 12, 11}; 131 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 132 | 133 | unsigned int size = 1; 134 | 135 | Test("Test5", vecNumbers, size, vecExpected); 136 | } 137 | 138 | // size of sliding windows is same as the array length 139 | void Test6() 140 | { 141 | int num[] = {10, 14, 12, 11}; 142 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 143 | 144 | int expected[] = {14}; 145 | vector vecExpected(expected, expected + sizeof(expected) / sizeof(int)); 146 | 147 | unsigned int size = 4; 148 | 149 | Test("Test6", vecNumbers, size, vecExpected); 150 | } 151 | 152 | // size of sliding windows is 0 153 | void Test7() 154 | { 155 | int num[] = {10, 14, 12, 11}; 156 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 157 | 158 | vector vecExpected; 159 | 160 | unsigned int size = 0; 161 | 162 | Test("Test7", vecNumbers, size, vecExpected); 163 | } 164 | 165 | // size of sliding windows is greater than the array length 166 | void Test8() 167 | { 168 | int num[] = {10, 14, 12, 11}; 169 | vector vecNumbers(num, num + sizeof(num) / sizeof(int)); 170 | 171 | vector vecExpected; 172 | 173 | unsigned int size = 5; 174 | 175 | Test("Test8", vecNumbers, size, vecExpected); 176 | } 177 | 178 | int main(int argc, char* argv[]) 179 | { 180 | Test1(); 181 | Test2(); 182 | Test3(); 183 | Test4(); 184 | Test5(); 185 | Test6(); 186 | Test7(); 187 | Test8(); 188 | 189 | return 0; 190 | } 191 | 192 | -------------------------------------------------------------------------------- /MaxInSlidingWindow/MaxInSlidingWindow.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {0720198D-FE39-49EF-9560-2E8FA9D7F570} 15 | Win32Proj 16 | MaxInSlidingWindow 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /MaxInSlidingWindow/MaxInSlidingWindow.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /NextNodeInBinaryTrees/NextNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 18 in ) Given a node in a binary tree, please implement a function to retrieve 4 | * its next node in the inorder traversal sequence. There is a pointer to the parent node in each tree node. 5 | */ 6 | 7 | #include 8 | 9 | struct BinaryTreeNode 10 | { 11 | int m_nValue; 12 | BinaryTreeNode* m_pLeft; 13 | BinaryTreeNode* m_pRight; 14 | BinaryTreeNode* m_pParent; 15 | }; 16 | 17 | BinaryTreeNode* GetNext(BinaryTreeNode* pNode) 18 | { 19 | if(pNode == NULL) 20 | return NULL; 21 | 22 | BinaryTreeNode* pNext = NULL; 23 | if(pNode->m_pRight != NULL) 24 | { 25 | BinaryTreeNode* pRight = pNode->m_pRight; 26 | while(pRight->m_pLeft != NULL) 27 | pRight = pRight->m_pLeft; 28 | 29 | pNext = pRight; 30 | } 31 | else if(pNode->m_pParent != NULL) 32 | { 33 | BinaryTreeNode* pCurrent = pNode; 34 | BinaryTreeNode* pParent = pNode->m_pParent; 35 | while(pParent != NULL && pCurrent == pParent->m_pRight) 36 | { 37 | pCurrent = pParent; 38 | pParent = pParent->m_pParent; 39 | } 40 | 41 | pNext = pParent; 42 | } 43 | 44 | return pNext; 45 | } 46 | 47 | // ==================== Code for Binary Trees ==================== 48 | BinaryTreeNode* CreateBinaryTreeNode(int value) 49 | { 50 | BinaryTreeNode* pNode = new BinaryTreeNode(); 51 | pNode->m_nValue = value; 52 | pNode->m_pLeft = NULL; 53 | pNode->m_pRight = NULL; 54 | pNode->m_pParent = NULL; 55 | 56 | return pNode; 57 | } 58 | 59 | void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight) 60 | { 61 | if(pParent != NULL) 62 | { 63 | pParent->m_pLeft = pLeft; 64 | pParent->m_pRight = pRight; 65 | 66 | if(pLeft != NULL) 67 | pLeft->m_pParent = pParent; 68 | if(pRight != NULL) 69 | pRight->m_pParent = pParent; 70 | } 71 | } 72 | 73 | void PrintTreeNode(BinaryTreeNode* pNode) 74 | { 75 | if(pNode != NULL) 76 | { 77 | printf("value of this node is: %d\n", pNode->m_nValue); 78 | 79 | if(pNode->m_pLeft != NULL) 80 | printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue); 81 | else 82 | printf("left child is null.\n"); 83 | 84 | if(pNode->m_pRight != NULL) 85 | printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue); 86 | else 87 | printf("right child is null.\n"); 88 | } 89 | else 90 | { 91 | printf("this node is null.\n"); 92 | } 93 | 94 | printf("\n"); 95 | } 96 | 97 | void PrintTree(BinaryTreeNode* pRoot) 98 | { 99 | PrintTreeNode(pRoot); 100 | 101 | if(pRoot != NULL) 102 | { 103 | if(pRoot->m_pLeft != NULL) 104 | PrintTree(pRoot->m_pLeft); 105 | 106 | if(pRoot->m_pRight != NULL) 107 | PrintTree(pRoot->m_pRight); 108 | } 109 | } 110 | 111 | void DestroyTree(BinaryTreeNode* pRoot) 112 | { 113 | if(pRoot != NULL) 114 | { 115 | BinaryTreeNode* pLeft = pRoot->m_pLeft; 116 | BinaryTreeNode* pRight = pRoot->m_pRight; 117 | 118 | delete pRoot; 119 | pRoot = NULL; 120 | 121 | DestroyTree(pLeft); 122 | DestroyTree(pRight); 123 | } 124 | } 125 | 126 | // ==================== Test Code ==================== 127 | void Test(char* testName, BinaryTreeNode* pNode, BinaryTreeNode* expected) 128 | { 129 | if(testName != NULL) 130 | printf("%s begins: ", testName); 131 | 132 | BinaryTreeNode* pNext = GetNext(pNode); 133 | if(pNext == expected) 134 | printf("Passed.\n"); 135 | else 136 | printf("FAILED.\n"); 137 | } 138 | 139 | // 8 140 | // 6 10 141 | // 5 7 9 11 142 | void Test1_7() 143 | { 144 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 145 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 146 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 147 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 148 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 149 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 150 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 151 | 152 | ConnectTreeNodes(pNode8, pNode6, pNode10); 153 | ConnectTreeNodes(pNode6, pNode5, pNode7); 154 | ConnectTreeNodes(pNode10, pNode9, pNode11); 155 | 156 | Test("Test1", pNode8, pNode9); 157 | Test("Test2", pNode6, pNode7); 158 | Test("Test3", pNode10, pNode11); 159 | Test("Test4", pNode5, pNode6); 160 | Test("Test5", pNode7, pNode8); 161 | Test("Test6", pNode9, pNode10); 162 | Test("Test7", pNode11, NULL); 163 | 164 | DestroyTree(pNode8); 165 | } 166 | 167 | // 5 168 | // 4 169 | // 3 170 | // 2 171 | void Test8_11() 172 | { 173 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 174 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 175 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 176 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 177 | 178 | ConnectTreeNodes(pNode5, pNode4, NULL); 179 | ConnectTreeNodes(pNode4, pNode3, NULL); 180 | ConnectTreeNodes(pNode3, pNode2, NULL); 181 | 182 | Test("Test8", pNode5, NULL); 183 | Test("Test9", pNode4, pNode5); 184 | Test("Test10", pNode3, pNode4); 185 | Test("Test11", pNode2, pNode3); 186 | 187 | DestroyTree(pNode5); 188 | } 189 | 190 | // 2 191 | // 3 192 | // 4 193 | // 5 194 | void Test12_15() 195 | { 196 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 197 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 198 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 199 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 200 | 201 | ConnectTreeNodes(pNode2, NULL, pNode3); 202 | ConnectTreeNodes(pNode3, NULL, pNode4); 203 | ConnectTreeNodes(pNode4, NULL, pNode5); 204 | 205 | Test("Test12", pNode5, NULL); 206 | Test("Test13", pNode4, pNode5); 207 | Test("Test14", pNode3, pNode4); 208 | Test("Test15", pNode2, pNode3); 209 | 210 | DestroyTree(pNode2); 211 | } 212 | 213 | void Test16() 214 | { 215 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 216 | 217 | Test("Test16", pNode5, NULL); 218 | 219 | DestroyTree(pNode5); 220 | } 221 | 222 | int main(int argc, char* argv[]) 223 | { 224 | Test1_7(); 225 | Test8_11(); 226 | Test12_15(); 227 | Test16(); 228 | } -------------------------------------------------------------------------------- /NextNodeInBinaryTrees/NextNodeInBinaryTrees.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {8CF39E25-265A-4D79-B63C-E3F646643D10} 15 | Win32Proj 16 | NextNodeInBinaryTrees 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /NextNodeInBinaryTrees/NextNodeInBinaryTrees.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /NumericStrings/NumericString.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 12 in ) How do you check whether a string stands for a number or not? 4 | * Numbers include positive and negative integers and floats. For example, strings "+100.", "5e2", "-.123", "3.1416", and "-1E-16" 5 | * stand for numbers, but "12e", "1a3.14", "1.2.3", "+-5", and "12e+5.4" do not. 6 | */ 7 | 8 | #include 9 | 10 | bool scanUnsignedInteger(const char** str); 11 | bool scanInteger(const char** str); 12 | 13 | // the form of exponent is like A[.[B]][e|EC] or .B[e|EC] 14 | // where A and C is an integer with or without sign, and B is an unsigned integer 15 | bool isNumeric(const char* str) 16 | { 17 | if (str == NULL) 18 | return false; 19 | 20 | bool numeric = scanInteger(&str); 21 | 22 | // for floats 23 | // you can have no integer part such as .123 means 0.123 24 | // in the meanwhile, you can have no decimal part such as 233. means 233.0 25 | // what's more, 233.666 is OK also. 26 | if (*str == '.') { 27 | ++str; 28 | numeric = scanUnsignedInteger(&str) || numeric; 29 | } 30 | 31 | // for exponent 32 | if (*str == 'e' || *str == 'E') { 33 | ++str; 34 | numeric = scanInteger(&str) && numeric; 35 | } 36 | 37 | return numeric && *str == '\0'; 38 | } 39 | 40 | bool scanUnsignedInteger(const char** str) 41 | { 42 | const char* pBefore = *str; 43 | while (**str != '\0' && **str >= '0' && **str <= '9') 44 | ++(*str); 45 | 46 | // return true when there are some digits in str 47 | return *str > pBefore; 48 | } 49 | 50 | // an integer's form is like [+|-]B, where B is an unsigned integer 51 | bool scanInteger(const char** str) 52 | { 53 | if (**str == '+' || **str == '-') 54 | ++(*str); 55 | return scanUnsignedInteger(str); 56 | } 57 | 58 | // ==================== Test Code ==================== 59 | 60 | void Test(const char* testName, const char* str, bool expected) 61 | { 62 | if (testName != NULL) 63 | printf("%s begins: ", testName); 64 | 65 | if (isNumeric(str) == expected) 66 | printf("Passed.\n"); 67 | else 68 | printf("FAILED.\n"); 69 | } 70 | 71 | 72 | int main(int argc, char* argv[]) 73 | { 74 | Test("Test1", "100", true); 75 | Test("Test2", "123.45e+6", true); 76 | Test("Test3", "+500", true); 77 | Test("Test4", "5e2", true); 78 | Test("Test5", "3.1416", true); 79 | Test("Test6", "600.", true); 80 | Test("Test7", "-.123", true); 81 | Test("Test8", "-1E-16", true); 82 | Test("Test9", "1.79769313486232E+308", true); 83 | 84 | printf("\n\n"); 85 | 86 | Test("Test10", "12e", false); 87 | Test("Test11", "1a3.14", false); 88 | Test("Test12", "1+23", false); 89 | Test("Test13", "1.2.3", false); 90 | Test("Test14", "+-5", false); 91 | Test("Test15", "12e+5.4", false); 92 | Test("Test16", ".", false); 93 | Test("Test17", ".e1", false); 94 | Test("Test18", "+.", false); 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /NumericStrings/NumericStrings.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {47891C37-4AB1-4332-AD03-ED1DBE8A978C} 15 | Win32Proj 16 | NumericStrings 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /NumericStrings/NumericStrings.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /PrintTreesInLines/PrintTreesInLines.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 58 in ) How do you print a binary tree by level, in top down order, 4 | * with each level in a line? Nodes in a level should be printed from left to right. 5 | */ 6 | 7 | #include 8 | #include "..\Utilities\BinaryTree.h" 9 | #include 10 | 11 | void Print(BinaryTreeNode* pRoot) 12 | { 13 | if(pRoot == NULL) 14 | return; 15 | 16 | std::queue nodes; 17 | nodes.push(pRoot); 18 | int nextLevel = 0; 19 | int toBePrinted = 1; 20 | while(!nodes.empty()) 21 | { 22 | BinaryTreeNode* pNode = nodes.front(); 23 | printf("%d ", pNode->m_nValue); 24 | 25 | if(pNode->m_pLeft != NULL) 26 | { 27 | nodes.push(pNode->m_pLeft); 28 | ++nextLevel; 29 | } 30 | if(pNode->m_pRight != NULL) 31 | { 32 | nodes.push(pNode->m_pRight); 33 | ++nextLevel; 34 | } 35 | 36 | nodes.pop(); 37 | --toBePrinted; 38 | if(toBePrinted == 0) 39 | { 40 | printf("\n"); 41 | toBePrinted = nextLevel; 42 | nextLevel = 0; 43 | } 44 | } 45 | } 46 | 47 | // ==================== Test Code ==================== 48 | // 8 49 | // 6 10 50 | // 5 7 9 11 51 | void Test1() 52 | { 53 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 54 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 55 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 56 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 57 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 58 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 59 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 60 | 61 | ConnectTreeNodes(pNode8, pNode6, pNode10); 62 | ConnectTreeNodes(pNode6, pNode5, pNode7); 63 | ConnectTreeNodes(pNode10, pNode9, pNode11); 64 | 65 | printf("====Test1 Begins: ====\n"); 66 | printf("Expected Result is:\n"); 67 | printf("8 \n"); 68 | printf("6 10 \n"); 69 | printf("5 7 9 11 \n\n"); 70 | 71 | printf("Actual Result is: \n"); 72 | Print(pNode8); 73 | printf("\n"); 74 | 75 | DestroyTree(pNode8); 76 | } 77 | 78 | // 5 79 | // 4 80 | // 3 81 | // 2 82 | void Test2() 83 | { 84 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 85 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 86 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 87 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 88 | 89 | ConnectTreeNodes(pNode5, pNode4, NULL); 90 | ConnectTreeNodes(pNode4, pNode3, NULL); 91 | ConnectTreeNodes(pNode3, pNode2, NULL); 92 | 93 | printf("====Test2 Begins: ====\n"); 94 | printf("Expected Result is:\n"); 95 | printf("5 \n"); 96 | printf("4 \n"); 97 | printf("3 \n"); 98 | printf("2 \n\n"); 99 | 100 | printf("Actual Result is: \n"); 101 | Print(pNode5); 102 | printf("\n"); 103 | 104 | DestroyTree(pNode5); 105 | } 106 | 107 | // 5 108 | // 4 109 | // 3 110 | // 2 111 | void Test3() 112 | { 113 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 114 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 115 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 116 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 117 | 118 | ConnectTreeNodes(pNode5, NULL, pNode4); 119 | ConnectTreeNodes(pNode4, NULL, pNode3); 120 | ConnectTreeNodes(pNode3, NULL, pNode2); 121 | 122 | printf("====Test3 Begins: ====\n"); 123 | printf("Expected Result is:\n"); 124 | printf("5 \n"); 125 | printf("4 \n"); 126 | printf("3 \n"); 127 | printf("2 \n\n"); 128 | 129 | printf("Actual Result is: \n"); 130 | Print(pNode5); 131 | printf("\n"); 132 | 133 | DestroyTree(pNode5); 134 | } 135 | 136 | void Test4() 137 | { 138 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 139 | 140 | printf("====Test4 Begins: ====\n"); 141 | printf("Expected Result is:\n"); 142 | printf("5 \n\n"); 143 | 144 | printf("Actual Result is: \n"); 145 | Print(pNode5); 146 | printf("\n"); 147 | 148 | DestroyTree(pNode5); 149 | } 150 | 151 | void Test5() 152 | { 153 | printf("====Test5 Begins: ====\n"); 154 | printf("Expected Result is:\n"); 155 | 156 | printf("Actual Result is: \n"); 157 | Print(NULL); 158 | printf("\n"); 159 | } 160 | 161 | // 100 162 | // / 163 | // 50 164 | // \ 165 | // 150 166 | void Test6() 167 | { 168 | BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100); 169 | BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50); 170 | BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150); 171 | 172 | ConnectTreeNodes(pNode100, pNode50, NULL); 173 | ConnectTreeNodes(pNode50, NULL, pNode150); 174 | 175 | printf("====Test6 Begins: ====\n"); 176 | printf("Expected Result is:\n"); 177 | printf("100 \n"); 178 | printf("50 \n"); 179 | printf("150 \n\n"); 180 | 181 | printf("Actual Result is: \n"); 182 | Print(pNode100); 183 | printf("\n"); 184 | } 185 | 186 | int main(int argc, char* argv[]) 187 | { 188 | Test1(); 189 | Test2(); 190 | Test3(); 191 | Test4(); 192 | Test5(); 193 | Test6(); 194 | 195 | return 0; 196 | } 197 | -------------------------------------------------------------------------------- /PrintTreesInLines/PrintTreesInLines.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {B1DB40C3-7E7D-4FFB-AEE3-921E39118606} 15 | Win32Proj 16 | PrintTreesInLines 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /PrintTreesInLines/PrintTreesInLines.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /PrintTreesInZigzag/PrintTreesInZigzag.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {928E5522-02BB-493A-9F1A-EC25F285F833} 15 | Win32Proj 16 | PrintTreesInZigzag 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /PrintTreesInZigzag/PrintTreesInZigzag.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /PrintTreesInZigzag/PrintTreesInzigzag.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 59 in ) How do you print a binary tree by level in zigzag order, 4 | * each level in a line? That is to say, nodes on the first level are printed from left to right, 5 | * nodes on the second level are printed from right to left, nodes on the third level are printed 6 | * from left to right again, and so on. 7 | */ 8 | 9 | #include 10 | #include "..\Utilities\BinaryTree.h" 11 | #include 12 | 13 | void Print(BinaryTreeNode* pRoot) 14 | { 15 | if(pRoot == NULL) 16 | return; 17 | 18 | std::stack levels[2]; 19 | int current = 0; 20 | int next = 1; 21 | 22 | levels[current].push(pRoot); 23 | while(!levels[0].empty() || !levels[1].empty()) 24 | { 25 | BinaryTreeNode* pNode = levels[current].top(); 26 | levels[current].pop(); 27 | 28 | printf("%d ", pNode->m_nValue); 29 | 30 | if(current == 0) 31 | { 32 | if(pNode->m_pLeft != NULL) 33 | levels[next].push(pNode->m_pLeft); 34 | if(pNode->m_pRight != NULL) 35 | levels[next].push(pNode->m_pRight); 36 | } 37 | else 38 | { 39 | if(pNode->m_pRight != NULL) 40 | levels[next].push(pNode->m_pRight); 41 | if(pNode->m_pLeft != NULL) 42 | levels[next].push(pNode->m_pLeft); 43 | } 44 | 45 | if(levels[current].empty()) 46 | { 47 | printf("\n"); 48 | current = 1 - current; 49 | next = 1 - next; 50 | } 51 | } 52 | } 53 | 54 | // 8 55 | // 6 10 56 | // 5 7 9 11 57 | void Test1() 58 | { 59 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 60 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 61 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 62 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 63 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 64 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 65 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 66 | 67 | ConnectTreeNodes(pNode8, pNode6, pNode10); 68 | ConnectTreeNodes(pNode6, pNode5, pNode7); 69 | ConnectTreeNodes(pNode10, pNode9, pNode11); 70 | 71 | printf("====Test1 Begins: ====\n"); 72 | printf("Expected Result is:\n"); 73 | printf("8 \n"); 74 | printf("10 6 \n"); 75 | printf("5 7 9 11 \n\n"); 76 | 77 | printf("Actual Result is: \n"); 78 | Print(pNode8); 79 | printf("\n"); 80 | 81 | DestroyTree(pNode8); 82 | } 83 | 84 | // 5 85 | // 4 86 | // 3 87 | // 2 88 | void Test2() 89 | { 90 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 91 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 92 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 93 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 94 | 95 | ConnectTreeNodes(pNode5, pNode4, NULL); 96 | ConnectTreeNodes(pNode4, pNode3, NULL); 97 | ConnectTreeNodes(pNode3, pNode2, NULL); 98 | 99 | printf("====Test2 Begins: ====\n"); 100 | printf("Expected Result is:\n"); 101 | printf("5 \n"); 102 | printf("4 \n"); 103 | printf("3 \n"); 104 | printf("2 \n\n"); 105 | 106 | printf("Actual Result is: \n"); 107 | Print(pNode5); 108 | printf("\n"); 109 | 110 | DestroyTree(pNode5); 111 | } 112 | 113 | // 5 114 | // 4 115 | // 3 116 | // 2 117 | void Test3() 118 | { 119 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 120 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 121 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 122 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 123 | 124 | ConnectTreeNodes(pNode5, NULL, pNode4); 125 | ConnectTreeNodes(pNode4, NULL, pNode3); 126 | ConnectTreeNodes(pNode3, NULL, pNode2); 127 | 128 | printf("====Test3 Begins: ====\n"); 129 | printf("Expected Result is:\n"); 130 | printf("5 \n"); 131 | printf("4 \n"); 132 | printf("3 \n"); 133 | printf("2 \n\n"); 134 | 135 | printf("Actual Result is: \n"); 136 | Print(pNode5); 137 | printf("\n"); 138 | 139 | DestroyTree(pNode5); 140 | } 141 | 142 | void Test4() 143 | { 144 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 145 | 146 | printf("====Test4 Begins: ====\n"); 147 | printf("Expected Result is:\n"); 148 | printf("5 \n\n"); 149 | 150 | printf("Actual Result is: \n"); 151 | Print(pNode5); 152 | printf("\n"); 153 | 154 | DestroyTree(pNode5); 155 | } 156 | 157 | void Test5() 158 | { 159 | printf("====Test5 Begins: ====\n"); 160 | printf("Expected Result is:\n"); 161 | 162 | printf("Actual Result is: \n"); 163 | Print(NULL); 164 | printf("\n"); 165 | } 166 | 167 | // 100 168 | // / 169 | // 50 170 | // \ 171 | // 150 172 | void Test6() 173 | { 174 | BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100); 175 | BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50); 176 | BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150); 177 | 178 | ConnectTreeNodes(pNode100, pNode50, NULL); 179 | ConnectTreeNodes(pNode50, NULL, pNode150); 180 | 181 | printf("====Test6 Begins: ====\n"); 182 | printf("Expected Result is:\n"); 183 | printf("100 \n"); 184 | printf("50 \n"); 185 | printf("150 \n\n"); 186 | 187 | printf("Actual Result is: \n"); 188 | Print(pNode100); 189 | printf("\n"); 190 | } 191 | 192 | // 8 193 | // 4 12 194 | // 2 6 10 14 195 | // 1 3 5 7 9 11 13 15 196 | void Test7() 197 | { 198 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 199 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 200 | BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); 201 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 202 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 203 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 204 | BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); 205 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 206 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 207 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 208 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 209 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 210 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 211 | BinaryTreeNode* pNode13 = CreateBinaryTreeNode(13); 212 | BinaryTreeNode* pNode15 = CreateBinaryTreeNode(15); 213 | 214 | ConnectTreeNodes(pNode8, pNode4, pNode12); 215 | ConnectTreeNodes(pNode4, pNode2, pNode6); 216 | ConnectTreeNodes(pNode12, pNode10, pNode14); 217 | ConnectTreeNodes(pNode2, pNode1, pNode3); 218 | ConnectTreeNodes(pNode6, pNode5, pNode7); 219 | ConnectTreeNodes(pNode10, pNode9, pNode11); 220 | ConnectTreeNodes(pNode14, pNode13, pNode15); 221 | 222 | printf("====Test7 Begins: ====\n"); 223 | printf("Expected Result is:\n"); 224 | printf("8 \n"); 225 | printf("12 4 \n"); 226 | printf("2 6 10 14 \n"); 227 | printf("15 13 11 9 7 5 3 1 \n\n"); 228 | 229 | printf("Actual Result is: \n"); 230 | Print(pNode8); 231 | printf("\n"); 232 | 233 | DestroyTree(pNode8); 234 | } 235 | 236 | int main(int argc, char* argv[]) 237 | { 238 | Test1(); 239 | Test2(); 240 | Test3(); 241 | Test4(); 242 | Test5(); 243 | Test6(); 244 | Test7(); 245 | 246 | return 0; 247 | } 248 | 249 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ChineseCodingInterviewAppendix 2 | ============================== 3 | 4 | The source code for the appendix part of the Chinese version of the book Coding Interviews 5 | -------------------------------------------------------------------------------- /RegularExpressionsMatching/RegularExpressions.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhedahht/ChineseCodingInterviewAppendix/5e62cafd79bf261d7fb32dcc0e08a8d87dff8932/RegularExpressionsMatching/RegularExpressions.cpp -------------------------------------------------------------------------------- /RegularExpressionsMatching/RegularExpressionsMatching.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {3B6BF857-6BF9-42C0-9EF2-2898B7633AE7} 15 | Win32Proj 16 | RegularExpressionsMatching 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /RegularExpressionsMatching/RegularExpressionsMatching.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /RobotMove/RobotMove.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 31 in ) A robot starts at cell (0, 0) of a grid with m rows and n columns. 4 | * It can move to the left, right, up, and down, and moves one cell for a step. It cannot enter cells where 5 | * the digit sum of the row index and column index are greater than a given k. 6 | */ 7 | 8 | #include 9 | 10 | int getDigitSum(int number) 11 | { 12 | int sum = 0; 13 | while(number > 0) 14 | { 15 | sum += number % 10; 16 | number /= 10; 17 | } 18 | 19 | return sum; 20 | } 21 | 22 | bool check(int threshold, int rows, int cols, int row, int col, bool* visited) 23 | { 24 | if(row >=0 && row < rows && col >= 0 && col < cols 25 | && getDigitSum(row) + getDigitSum(col) <= threshold 26 | && !visited[row* cols + col]) 27 | return true; 28 | 29 | return false; 30 | } 31 | 32 | int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited) 33 | { 34 | int count = 0; 35 | if(check(threshold, rows, cols, row, col, visited)) 36 | { 37 | visited[row * cols + col] = true; 38 | 39 | count = 1 + movingCountCore(threshold, rows, cols, 40 | row - 1, col, visited) 41 | + movingCountCore(threshold, rows, cols, 42 | row, col - 1, visited) 43 | + movingCountCore(threshold, rows, cols, 44 | row + 1, col, visited) 45 | + movingCountCore(threshold, rows, cols, 46 | row, col + 1, visited); 47 | } 48 | 49 | return count; 50 | } 51 | 52 | int movingCount(int threshold, int rows, int cols) 53 | { 54 | bool *visited = new bool[rows * cols]; 55 | for(int i = 0; i < rows * cols; ++i) 56 | visited[i] = false; 57 | 58 | int count = movingCountCore(threshold, rows, cols, 59 | 0, 0, visited); 60 | 61 | delete[] visited; 62 | 63 | return count; 64 | } 65 | 66 | // ================================ test code ================================ 67 | void test(char* testName, int threshold, int rows, int cols, int expected) 68 | { 69 | if(testName != NULL) 70 | printf("%s begins: ", testName); 71 | 72 | if(movingCount(threshold, rows, cols) == expected) 73 | printf("Passed.\n"); 74 | else 75 | printf("FAILED.\n"); 76 | } 77 | 78 | void test1() 79 | { 80 | test("Test1", 5, 10, 10, 21); 81 | } 82 | 83 | void test2() 84 | { 85 | test("Test2", 15, 20, 20, 359); 86 | } 87 | 88 | void test3() 89 | { 90 | test("Test3", 10, 1, 100, 29); 91 | } 92 | 93 | void test4() 94 | { 95 | test("Test4", 10, 1, 10, 10); 96 | } 97 | 98 | void test5() 99 | { 100 | test("Test5", 15, 100, 1, 79); 101 | } 102 | 103 | void test6() 104 | { 105 | test("Test6", 15, 10, 1, 10); 106 | } 107 | 108 | void test7() 109 | { 110 | test("Test7", 15, 1, 1, 1); 111 | } 112 | 113 | void test8() 114 | { 115 | test("Test8", -10, 10, 10, 0); 116 | } 117 | 118 | int main(int agrc, char* argv[]) 119 | { 120 | test1(); 121 | test2(); 122 | test3(); 123 | test4(); 124 | test5(); 125 | test6(); 126 | test7(); 127 | test8(); 128 | } -------------------------------------------------------------------------------- /RobotMove/RobotMove.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {23720702-0117-4D66-9E67-2A609E28C083} 15 | Win32Proj 16 | RobotMove 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /RobotMove/RobotMove.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /SerializeBinaryTrees/SerializeBinaryTrees.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 62 in ) How do you serialize and deserialize binary trees? 4 | */ 5 | 6 | #include 7 | #include "..\Utilities\BinaryTree.h" 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | void Serialize(BinaryTreeNode* pRoot, ostream& stream) 14 | { 15 | if(pRoot == NULL) 16 | { 17 | stream << "$,"; 18 | return; 19 | } 20 | 21 | stream << pRoot->m_nValue << ','; 22 | Serialize(pRoot->m_pLeft, stream); 23 | Serialize(pRoot->m_pRight, stream); 24 | } 25 | 26 | bool ReadStream(istream& stream, int* number) 27 | { 28 | if(stream.eof()) 29 | return false; 30 | 31 | char buffer[32]; 32 | buffer[0] = '\0'; 33 | 34 | char ch; 35 | stream >> ch; 36 | int i = 0; 37 | while(!stream.eof() && ch != ',') 38 | { 39 | buffer[i++] = ch; 40 | stream >> ch; 41 | } 42 | 43 | bool isNumeric = false; 44 | if(i > 0 && buffer[0] != '$') 45 | { 46 | *number = atoi(buffer); 47 | isNumeric = true; 48 | } 49 | 50 | return isNumeric; 51 | } 52 | 53 | void Deserialize(BinaryTreeNode** pRoot, istream& stream) 54 | { 55 | int number; 56 | if(ReadStream(stream, &number)) 57 | { 58 | *pRoot = new BinaryTreeNode(); 59 | (*pRoot)->m_nValue = number; 60 | (*pRoot)->m_pLeft = NULL; 61 | (*pRoot)->m_pRight = NULL; 62 | 63 | Deserialize(&((*pRoot)->m_pLeft), stream); 64 | Deserialize(&((*pRoot)->m_pRight), stream); 65 | } 66 | } 67 | 68 | // ==================== Test Code ==================== 69 | bool isSameTree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) 70 | { 71 | if(pRoot1 == NULL && pRoot2 == NULL) 72 | return true; 73 | 74 | if(pRoot1 == NULL || pRoot2 == NULL) 75 | return false; 76 | 77 | if(pRoot1->m_nValue != pRoot2->m_nValue) 78 | return false; 79 | 80 | return isSameTree(pRoot1->m_pLeft, pRoot2->m_pLeft) && 81 | isSameTree(pRoot1->m_pRight, pRoot2->m_pRight); 82 | } 83 | 84 | void Test(char* testName, BinaryTreeNode* pRoot) 85 | { 86 | if(testName != NULL) 87 | printf("%s begins: \n", testName); 88 | 89 | PrintTree(pRoot); 90 | 91 | char* fileName = "test.txt"; 92 | ofstream fileOut; 93 | fileOut.open(fileName); 94 | 95 | Serialize(pRoot, fileOut); 96 | fileOut.close(); 97 | 98 | // print the serialized file 99 | ifstream fileIn1; 100 | char ch; 101 | fileIn1.open(fileName); 102 | while(!fileIn1.eof()) 103 | { 104 | fileIn1 >> ch; 105 | cout << ch; 106 | } 107 | fileIn1.close(); 108 | cout << endl; 109 | 110 | ifstream fileIn2; 111 | fileIn2.open(fileName); 112 | BinaryTreeNode* pNewRoot = NULL; 113 | Deserialize(&pNewRoot, fileIn2); 114 | fileIn2.close(); 115 | 116 | PrintTree(pNewRoot); 117 | 118 | if(isSameTree(pRoot, pNewRoot)) 119 | printf("The deserialized tree is same as the oritinal tree.\n\n"); 120 | else 121 | printf("The deserialized tree is NOT same as the oritinal tree.\n\n"); 122 | 123 | DestroyTree(pNewRoot); 124 | } 125 | 126 | // 8 127 | // 6 10 128 | // 5 7 9 11 129 | void Test1() 130 | { 131 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 132 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 133 | BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 134 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 135 | BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 136 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 137 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 138 | 139 | ConnectTreeNodes(pNode8, pNode6, pNode10); 140 | ConnectTreeNodes(pNode6, pNode5, pNode7); 141 | ConnectTreeNodes(pNode10, pNode9, pNode11); 142 | 143 | Test("Test1", pNode8); 144 | 145 | DestroyTree(pNode8); 146 | } 147 | 148 | // 5 149 | // 4 150 | // 3 151 | // 2 152 | void Test2() 153 | { 154 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 155 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 156 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 157 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 158 | 159 | ConnectTreeNodes(pNode5, pNode4, NULL); 160 | ConnectTreeNodes(pNode4, pNode3, NULL); 161 | ConnectTreeNodes(pNode3, pNode2, NULL); 162 | 163 | Test("Test2", pNode5); 164 | 165 | DestroyTree(pNode5); 166 | } 167 | 168 | // 5 169 | // 4 170 | // 3 171 | // 2 172 | void Test3() 173 | { 174 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 175 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 176 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 177 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 178 | 179 | ConnectTreeNodes(pNode5, NULL, pNode4); 180 | ConnectTreeNodes(pNode4, NULL, pNode3); 181 | ConnectTreeNodes(pNode3, NULL, pNode2); 182 | 183 | Test("Test3", pNode5); 184 | 185 | DestroyTree(pNode5); 186 | } 187 | 188 | void Test4() 189 | { 190 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 191 | 192 | Test("Test4", pNode5); 193 | 194 | DestroyTree(pNode5); 195 | } 196 | 197 | void Test5() 198 | { 199 | Test("Test5", NULL); 200 | } 201 | 202 | // 5 203 | // 5 204 | // 5 205 | // 5 206 | // 5 207 | // 5 5 208 | // 5 5 209 | void Test6() 210 | { 211 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(5); 212 | BinaryTreeNode* pNode2 = CreateBinaryTreeNode(5); 213 | BinaryTreeNode* pNode3 = CreateBinaryTreeNode(5); 214 | BinaryTreeNode* pNode4 = CreateBinaryTreeNode(5); 215 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 216 | BinaryTreeNode* pNode61 = CreateBinaryTreeNode(5); 217 | BinaryTreeNode* pNode62 = CreateBinaryTreeNode(5); 218 | BinaryTreeNode* pNode71 = CreateBinaryTreeNode(5); 219 | BinaryTreeNode* pNode72 = CreateBinaryTreeNode(5); 220 | 221 | ConnectTreeNodes(pNode1, NULL, pNode2); 222 | ConnectTreeNodes(pNode2, NULL, pNode3); 223 | ConnectTreeNodes(pNode3, pNode4, NULL); 224 | ConnectTreeNodes(pNode4, pNode5, NULL); 225 | ConnectTreeNodes(pNode5, pNode61, pNode62); 226 | ConnectTreeNodes(pNode61, pNode71, NULL); 227 | ConnectTreeNodes(pNode62, NULL, pNode72); 228 | 229 | Test("Test6", pNode1); 230 | 231 | DestroyTree(pNode1); 232 | } 233 | 234 | int main(int argc, char* argv[]) 235 | { 236 | Test1(); 237 | Test2(); 238 | Test3(); 239 | Test4(); 240 | Test5(); 241 | Test6(); 242 | 243 | return 0; 244 | } -------------------------------------------------------------------------------- /SerializeBinaryTrees/SerializeBinaryTrees.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {B9DB5738-C69C-4B80-9070-FF36B1E6F772} 15 | Win32Proj 16 | SerializeBinaryTrees 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /SerializeBinaryTrees/SerializeBinaryTrees.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /StreamMedian/StreamMedian.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 69 in ) How do you find the median from a stream of numbers? 4 | * The median of a set of numbers is the middle one when they are arranged in order. If the 5 | * count of numbers is even, the median is defined as the average value of the two numbers in the middle. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | template class DynamicArray 16 | { 17 | public: 18 | void Insert(T num) 19 | { 20 | if(((min.size() + max.size()) & 1) == 0) 21 | { 22 | if(max.size() > 0 && num < max[0]) 23 | { 24 | max.push_back(num); 25 | push_heap(max.begin(), max.end(), less()); 26 | 27 | num = max[0]; 28 | 29 | pop_heap(max.begin(), max.end(), less()); 30 | max.pop_back(); 31 | } 32 | 33 | min.push_back(num); 34 | push_heap(min.begin(), min.end(), greater()); 35 | } 36 | else 37 | { 38 | if(min.size() > 0 && min[0] < num) 39 | { 40 | min.push_back(num); 41 | push_heap(min.begin(), min.end(), greater()); 42 | 43 | num = min[0]; 44 | 45 | pop_heap(min.begin(), min.end(), greater()); 46 | min.pop_back(); 47 | } 48 | 49 | max.push_back(num); 50 | push_heap(max.begin(), max.end(), less()); 51 | } 52 | } 53 | 54 | T GetMedian() 55 | { 56 | int size = min.size() + max.size(); 57 | if(size == 0) 58 | throw exception("No numbers are available"); 59 | 60 | T median = 0; 61 | if((size & 1) == 1) 62 | median = min[0]; 63 | else 64 | median = (min[0] + max[0]) / 2; 65 | 66 | return median; 67 | } 68 | 69 | private: 70 | vector min; 71 | vector max; 72 | }; 73 | 74 | // ==================== Test Code ==================== 75 | void Test(char* testName, DynamicArray& numbers, double expected) 76 | { 77 | if(testName != NULL) 78 | printf("%s begins: ", testName); 79 | 80 | if(abs(numbers.GetMedian() - expected) < 0.0000001) 81 | printf("Passed.\n"); 82 | else 83 | printf("FAILED.\n"); 84 | } 85 | 86 | int main(int argc, char* argv[]) 87 | { 88 | DynamicArray numbers; 89 | 90 | printf("Test1 begins: "); 91 | try 92 | { 93 | numbers.GetMedian(); 94 | printf("FAILED.\n"); 95 | } 96 | catch(exception e) 97 | { 98 | printf("Passed.\n"); 99 | } 100 | 101 | numbers.Insert(5); 102 | Test("Test2", numbers, 5); 103 | 104 | numbers.Insert(2); 105 | Test("Test3", numbers, 3.5); 106 | 107 | numbers.Insert(3); 108 | Test("Test4", numbers, 3); 109 | 110 | numbers.Insert(4); 111 | Test("Test6", numbers, 3.5); 112 | 113 | numbers.Insert(1); 114 | Test("Test5", numbers, 3); 115 | 116 | numbers.Insert(6); 117 | Test("Test7", numbers, 3.5); 118 | 119 | numbers.Insert(7); 120 | Test("Test8", numbers, 4); 121 | 122 | numbers.Insert(0); 123 | Test("Test9", numbers, 3.5); 124 | 125 | numbers.Insert(8); 126 | Test("Test10", numbers, 4); 127 | 128 | return 0; 129 | } 130 | 131 | -------------------------------------------------------------------------------- /StreamMedian/StreamMedian.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {F85D00AB-0A01-4BB7-9BD9-E7786A9517CA} 15 | Win32Proj 16 | StreamMedian 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /StreamMedian/StreamMedian.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /StringPathInMatrix/StringPath.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 30 in ) How do you implement a function to check whether there is a 4 | * path for a string in a matrix of characters? It moves to left, right, up, and down in a matrix, 5 | * and a cell for a step. The path can start from any cell in a matrix. If a cell is occupied by a 6 | * character of a string on the path, it cannot be occupied by another character again. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited); 16 | 17 | bool hasPath(char* matrix, int rows, int cols, char* str) 18 | { 19 | if(matrix == NULL || rows < 1 || cols < 1 || str == NULL) 20 | return false; 21 | 22 | bool *visited = new bool[rows * cols]; 23 | memset(visited, 0, rows * cols); 24 | 25 | int pathLength = 0; 26 | for(int row = 0; row < rows; ++row) 27 | { 28 | for(int col = 0; col < cols; ++col) 29 | { 30 | if(hasPathCore(matrix, rows, cols, row, col, str, 31 | pathLength, visited)) 32 | { 33 | return true; 34 | } 35 | } 36 | } 37 | 38 | delete[] visited; 39 | 40 | return false; 41 | } 42 | 43 | bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited) 44 | { 45 | if(str[pathLength] == '\0') 46 | return true; 47 | 48 | bool hasPath = false; 49 | if(row >= 0 && row < rows && col >= 0 && col < cols 50 | && matrix[row * cols + col] == str[pathLength] 51 | && !visited[row * cols + col]) 52 | { 53 | ++pathLength; 54 | visited[row * cols + col] = true; 55 | 56 | hasPath = hasPathCore(matrix, rows, cols, row, col - 1, 57 | str, pathLength, visited) 58 | || hasPathCore(matrix, rows, cols, row - 1, col, 59 | str, pathLength, visited) 60 | || hasPathCore(matrix, rows, cols, row, col + 1, 61 | str, pathLength, visited) 62 | || hasPathCore(matrix, rows, cols, row + 1, col, 63 | str, pathLength, visited); 64 | 65 | if(!hasPath) 66 | { 67 | --pathLength; 68 | visited[row * cols + col] = false; 69 | } 70 | } 71 | 72 | return hasPath; 73 | } 74 | 75 | void Test(char* testName, char* matrix, int rows, int cols, char* str, bool expected) 76 | { 77 | if(testName != NULL) 78 | printf("%s begins: ", testName); 79 | 80 | if(hasPath(matrix, rows, cols, str) == expected) 81 | printf("Passed.\n"); 82 | else 83 | printf("FAILED.\n"); 84 | } 85 | 86 | //ABCE 87 | //SFCS 88 | //ADEE 89 | 90 | //ABCCED 91 | void Test1() 92 | { 93 | char matrix[] = "ABCESFCSADEE"; 94 | char* str = "ABCCED"; 95 | 96 | Test("Test1", (char*)matrix, 3, 4, str, true); 97 | } 98 | 99 | //ABCE 100 | //SFCS 101 | //ADEE 102 | 103 | //SEE 104 | void Test2() 105 | { 106 | char matrix[] = "ABCESFCSADEE"; 107 | char* str = "SEE"; 108 | 109 | Test("Test2", (char*)matrix, 3, 4, str, true); 110 | } 111 | 112 | //ABCE 113 | //SFCS 114 | //ADEE 115 | 116 | //ABCB 117 | void Test3() 118 | { 119 | char matrix[] = "ABCESFCSADEE"; 120 | char* str = "ABCB"; 121 | 122 | Test("Test3", (char*)matrix, 3, 4, str, false); 123 | } 124 | 125 | //ABCEHJIG 126 | //SFCSLOPQ 127 | //ADEEMNOE 128 | //ADIDEJFM 129 | //VCEIFGGS 130 | 131 | //SLHECCEIDEJFGGFIE 132 | void Test4() 133 | { 134 | char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; 135 | char* str = "SLHECCEIDEJFGGFIE"; 136 | 137 | Test("Test4", (char*)matrix, 5, 8, str, true); 138 | } 139 | 140 | //ABCEHJIG 141 | //SFCSLOPQ 142 | //ADEEMNOE 143 | //ADIDEJFM 144 | //VCEIFGGS 145 | 146 | //SGGFIECVAASABCEHJIGQEM 147 | void Test5() 148 | { 149 | char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; 150 | char* str = "SGGFIECVAASABCEHJIGQEM"; 151 | 152 | Test("Test5", (char*)matrix, 5, 8, str, true); 153 | } 154 | 155 | //ABCEHJIG 156 | //SFCSLOPQ 157 | //ADEEMNOE 158 | //ADIDEJFM 159 | //VCEIFGGS 160 | 161 | //SGGFIECVAASABCEEJIGOEM 162 | void Test6() 163 | { 164 | char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; 165 | char* str = "SGGFIECVAASABCEEJIGOEM"; 166 | 167 | Test("Test6", (char*)matrix, 5, 8, str, false); 168 | } 169 | 170 | //ABCEHJIG 171 | //SFCSLOPQ 172 | //ADEEMNOE 173 | //ADIDEJFM 174 | //VCEIFGGS 175 | 176 | //SGGFIECVAASABCEHJIGQEMS 177 | void Test7() 178 | { 179 | char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; 180 | char* str = "SGGFIECVAASABCEHJIGQEMS"; 181 | 182 | Test("Test7", (char*)matrix, 5, 8, str, false); 183 | } 184 | 185 | //AAAA 186 | //AAAA 187 | //AAAA 188 | 189 | //AAAAAAAAAAAA 190 | void Test8() 191 | { 192 | char matrix[] = "AAAAAAAAAAAA"; 193 | char* str = "AAAAAAAAAAAA"; 194 | 195 | Test("Test8", (char*)matrix, 3, 4, str, true); 196 | } 197 | 198 | //AAAA 199 | //AAAA 200 | //AAAA 201 | 202 | //AAAAAAAAAAAAA 203 | void Test9() 204 | { 205 | char matrix[] = "AAAAAAAAAAAA"; 206 | char* str = "AAAAAAAAAAAAA"; 207 | 208 | Test("Test9", (char*)matrix, 3, 4, str, false); 209 | } 210 | 211 | //A 212 | 213 | //A 214 | void Test10() 215 | { 216 | char matrix[] = "A"; 217 | char* str = "A"; 218 | 219 | Test("Test10", (char*)matrix, 1, 1, str, true); 220 | } 221 | 222 | //A 223 | 224 | //B 225 | void Test11() 226 | { 227 | char matrix[] = "A"; 228 | char* str = "B"; 229 | 230 | Test("Test11", (char*)matrix, 1, 1, str, false); 231 | } 232 | 233 | int main(int argc, char* argv[]) 234 | { 235 | Test1(); 236 | Test2(); 237 | Test3(); 238 | Test4(); 239 | Test5(); 240 | Test6(); 241 | Test7(); 242 | Test8(); 243 | Test9(); 244 | Test10(); 245 | Test11(); 246 | 247 | return 0; 248 | } -------------------------------------------------------------------------------- /StringPathInMatrix/StringPathInMatrix.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {1D4BD3F4-AC32-4B45-B6A5-927EE8757E89} 15 | Win32Proj 16 | StringPathInMatrix 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /StringPathInMatrix/StringPathInMatrix.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /SymmetricalBinaryTree/SymmetricalBinaryTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 52 in ) Please implement a function to verify whether a binary tree is symmetrical. 4 | * A tree is symmetrical if its mirrored image looks the same as the tree itself. 5 | */ 6 | 7 | #include 8 | #include "../Utilities/BinaryTree.h" 9 | 10 | bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2); 11 | 12 | bool isSymmetrical(BinaryTreeNode* pRoot) 13 | { 14 | return isSymmetrical(pRoot, pRoot); 15 | } 16 | 17 | bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) 18 | { 19 | if(pRoot1 == NULL && pRoot2 == NULL) 20 | return true; 21 | 22 | if(pRoot1 == NULL || pRoot2 == NULL) 23 | return false; 24 | 25 | if(pRoot1->m_nValue != pRoot2->m_nValue) 26 | return false; 27 | 28 | return isSymmetrical(pRoot1->m_pLeft, pRoot2->m_pRight) 29 | && isSymmetrical(pRoot1->m_pRight, pRoot2->m_pLeft); 30 | } 31 | 32 | // ==================== Test Code ==================== 33 | void Test(char* testName, BinaryTreeNode* pRoot, bool expected) 34 | { 35 | if(testName != NULL) 36 | printf("%s begins: ", testName); 37 | 38 | if(isSymmetrical(pRoot) == expected) 39 | printf("Passed.\n"); 40 | else 41 | printf("FAILED.\n"); 42 | } 43 | 44 | // 8 45 | // 6 6 46 | // 5 7 7 5 47 | void Test1() 48 | { 49 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 50 | BinaryTreeNode* pNode61 = CreateBinaryTreeNode(6); 51 | BinaryTreeNode* pNode62 = CreateBinaryTreeNode(6); 52 | BinaryTreeNode* pNode51 = CreateBinaryTreeNode(5); 53 | BinaryTreeNode* pNode71 = CreateBinaryTreeNode(7); 54 | BinaryTreeNode* pNode72 = CreateBinaryTreeNode(7); 55 | BinaryTreeNode* pNode52 = CreateBinaryTreeNode(5); 56 | 57 | ConnectTreeNodes(pNode8, pNode61, pNode62); 58 | ConnectTreeNodes(pNode61, pNode51, pNode71); 59 | ConnectTreeNodes(pNode62, pNode72, pNode52); 60 | 61 | Test("Test1", pNode8, true); 62 | 63 | DestroyTree(pNode8); 64 | } 65 | 66 | // 8 67 | // 6 9 68 | // 5 7 7 5 69 | void Test2() 70 | { 71 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 72 | BinaryTreeNode* pNode61 = CreateBinaryTreeNode(6); 73 | BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 74 | BinaryTreeNode* pNode51 = CreateBinaryTreeNode(5); 75 | BinaryTreeNode* pNode71 = CreateBinaryTreeNode(7); 76 | BinaryTreeNode* pNode72 = CreateBinaryTreeNode(7); 77 | BinaryTreeNode* pNode52 = CreateBinaryTreeNode(5); 78 | 79 | ConnectTreeNodes(pNode8, pNode61, pNode9); 80 | ConnectTreeNodes(pNode61, pNode51, pNode71); 81 | ConnectTreeNodes(pNode9, pNode72, pNode52); 82 | 83 | Test("Test2", pNode8, false); 84 | 85 | DestroyTree(pNode8); 86 | } 87 | 88 | // 8 89 | // 6 6 90 | // 5 7 7 91 | void Test3() 92 | { 93 | BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 94 | BinaryTreeNode* pNode61 = CreateBinaryTreeNode(6); 95 | BinaryTreeNode* pNode62 = CreateBinaryTreeNode(6); 96 | BinaryTreeNode* pNode51 = CreateBinaryTreeNode(5); 97 | BinaryTreeNode* pNode71 = CreateBinaryTreeNode(7); 98 | BinaryTreeNode* pNode72 = CreateBinaryTreeNode(7); 99 | 100 | ConnectTreeNodes(pNode8, pNode61, pNode62); 101 | ConnectTreeNodes(pNode61, pNode51, pNode71); 102 | ConnectTreeNodes(pNode62, pNode72, NULL); 103 | 104 | Test("Test3", pNode8, false); 105 | 106 | DestroyTree(pNode8); 107 | } 108 | 109 | // 5 110 | // / \ 111 | // 3 3 112 | // / \ 113 | // 4 4 114 | // / \ 115 | // 2 2 116 | // / \ 117 | // 1 1 118 | void Test4() 119 | { 120 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 121 | BinaryTreeNode* pNode31 = CreateBinaryTreeNode(3); 122 | BinaryTreeNode* pNode32 = CreateBinaryTreeNode(3); 123 | BinaryTreeNode* pNode41 = CreateBinaryTreeNode(4); 124 | BinaryTreeNode* pNode42 = CreateBinaryTreeNode(4); 125 | BinaryTreeNode* pNode21 = CreateBinaryTreeNode(2); 126 | BinaryTreeNode* pNode22 = CreateBinaryTreeNode(2); 127 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(1); 128 | BinaryTreeNode* pNode12 = CreateBinaryTreeNode(1); 129 | 130 | ConnectTreeNodes(pNode5, pNode31, pNode32); 131 | ConnectTreeNodes(pNode31, pNode41, NULL); 132 | ConnectTreeNodes(pNode32, NULL, pNode42); 133 | ConnectTreeNodes(pNode41, pNode21, NULL); 134 | ConnectTreeNodes(pNode42, NULL, pNode22); 135 | ConnectTreeNodes(pNode21, pNode11, NULL); 136 | ConnectTreeNodes(pNode22, NULL, pNode12); 137 | 138 | Test("Test4", pNode5, true); 139 | 140 | DestroyTree(pNode5); 141 | } 142 | 143 | 144 | // 5 145 | // / \ 146 | // 3 3 147 | // / \ 148 | // 4 4 149 | // / \ 150 | // 6 2 151 | // / \ 152 | // 1 1 153 | void Test5() 154 | { 155 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 156 | BinaryTreeNode* pNode31 = CreateBinaryTreeNode(3); 157 | BinaryTreeNode* pNode32 = CreateBinaryTreeNode(3); 158 | BinaryTreeNode* pNode41 = CreateBinaryTreeNode(4); 159 | BinaryTreeNode* pNode42 = CreateBinaryTreeNode(4); 160 | BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 161 | BinaryTreeNode* pNode22 = CreateBinaryTreeNode(2); 162 | BinaryTreeNode* pNode11 = CreateBinaryTreeNode(1); 163 | BinaryTreeNode* pNode12 = CreateBinaryTreeNode(1); 164 | 165 | ConnectTreeNodes(pNode5, pNode31, pNode32); 166 | ConnectTreeNodes(pNode31, pNode41, NULL); 167 | ConnectTreeNodes(pNode32, NULL, pNode42); 168 | ConnectTreeNodes(pNode41, pNode6, NULL); 169 | ConnectTreeNodes(pNode42, NULL, pNode22); 170 | ConnectTreeNodes(pNode6, pNode11, NULL); 171 | ConnectTreeNodes(pNode22, NULL, pNode12); 172 | 173 | Test("Test5", pNode5, false); 174 | 175 | DestroyTree(pNode5); 176 | } 177 | 178 | // 5 179 | // / \ 180 | // 3 3 181 | // / \ 182 | // 4 4 183 | // / \ 184 | // 2 2 185 | // \ 186 | // 1 187 | void Test6() 188 | { 189 | BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 190 | BinaryTreeNode* pNode31 = CreateBinaryTreeNode(3); 191 | BinaryTreeNode* pNode32 = CreateBinaryTreeNode(3); 192 | BinaryTreeNode* pNode41 = CreateBinaryTreeNode(4); 193 | BinaryTreeNode* pNode42 = CreateBinaryTreeNode(4); 194 | BinaryTreeNode* pNode21 = CreateBinaryTreeNode(2); 195 | BinaryTreeNode* pNode22 = CreateBinaryTreeNode(2); 196 | BinaryTreeNode* pNode12 = CreateBinaryTreeNode(1); 197 | 198 | ConnectTreeNodes(pNode5, pNode31, pNode32); 199 | ConnectTreeNodes(pNode31, pNode41, NULL); 200 | ConnectTreeNodes(pNode32, NULL, pNode42); 201 | ConnectTreeNodes(pNode41, pNode21, NULL); 202 | ConnectTreeNodes(pNode42, NULL, pNode22); 203 | ConnectTreeNodes(pNode21, NULL, NULL); 204 | ConnectTreeNodes(pNode22, NULL, pNode12); 205 | 206 | Test("Test6", pNode5, false); 207 | 208 | DestroyTree(pNode5); 209 | } 210 | 211 | // Only one node 212 | void Test7() 213 | { 214 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 215 | Test("Test7", pNode1, true); 216 | 217 | DestroyTree(pNode1); 218 | } 219 | 220 | // No nodes 221 | void Test8() 222 | { 223 | Test("Test8", NULL, true); 224 | } 225 | 226 | // All nodes have the same value 227 | // 5 228 | // / \ 229 | // 5 5 230 | // / \ 231 | // 5 5 232 | // / \ 233 | // 5 5 234 | void Test9() 235 | { 236 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(5); 237 | BinaryTreeNode* pNode21 = CreateBinaryTreeNode(5); 238 | BinaryTreeNode* pNode22 = CreateBinaryTreeNode(5); 239 | BinaryTreeNode* pNode31 = CreateBinaryTreeNode(5); 240 | BinaryTreeNode* pNode32 = CreateBinaryTreeNode(5); 241 | BinaryTreeNode* pNode41 = CreateBinaryTreeNode(5); 242 | BinaryTreeNode* pNode42 = CreateBinaryTreeNode(5); 243 | 244 | ConnectTreeNodes(pNode1, pNode21, pNode22); 245 | ConnectTreeNodes(pNode21, pNode31, NULL); 246 | ConnectTreeNodes(pNode22, NULL, pNode32); 247 | ConnectTreeNodes(pNode31, pNode41, NULL); 248 | ConnectTreeNodes(pNode32, NULL, pNode42); 249 | ConnectTreeNodes(pNode41, NULL, NULL); 250 | ConnectTreeNodes(pNode42, NULL, NULL); 251 | 252 | Test("Test9", pNode1, true); 253 | 254 | DestroyTree(pNode1); 255 | } 256 | 257 | // All nodes have the same value 258 | // 5 259 | // / \ 260 | // 5 5 261 | // / \ 262 | // 5 5 263 | // / / 264 | // 5 5 265 | void Test10() 266 | { 267 | BinaryTreeNode* pNode1 = CreateBinaryTreeNode(5); 268 | BinaryTreeNode* pNode21 = CreateBinaryTreeNode(5); 269 | BinaryTreeNode* pNode22 = CreateBinaryTreeNode(5); 270 | BinaryTreeNode* pNode31 = CreateBinaryTreeNode(5); 271 | BinaryTreeNode* pNode32 = CreateBinaryTreeNode(5); 272 | BinaryTreeNode* pNode41 = CreateBinaryTreeNode(5); 273 | BinaryTreeNode* pNode42 = CreateBinaryTreeNode(5); 274 | 275 | ConnectTreeNodes(pNode1, pNode21, pNode22); 276 | ConnectTreeNodes(pNode21, pNode31, NULL); 277 | ConnectTreeNodes(pNode22, NULL, pNode32); 278 | ConnectTreeNodes(pNode31, pNode41, NULL); 279 | ConnectTreeNodes(pNode32, pNode42, NULL); 280 | ConnectTreeNodes(pNode41, NULL, NULL); 281 | ConnectTreeNodes(pNode42, NULL, NULL); 282 | 283 | Test("Test10", pNode1, false); 284 | 285 | DestroyTree(pNode1); 286 | } 287 | 288 | void main(int argc, char* argv[]) 289 | { 290 | Test1(); 291 | Test2(); 292 | Test3(); 293 | Test4(); 294 | Test5(); 295 | Test6(); 296 | Test7(); 297 | Test8(); 298 | Test9(); 299 | Test10(); 300 | } -------------------------------------------------------------------------------- /SymmetricalBinaryTree/SymmetricalBinaryTree.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {BCEB5FAA-BDD6-4B80-8D29-34418F900EAB} 15 | Win32Proj 16 | SymmetricalBinaryTree 17 | 18 | 19 | 20 | Application 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 57 | 58 | 59 | Console 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 75 | 76 | 77 | Console 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {715e6a55-16b3-4f05-9286-6dbc78da51e4} 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /SymmetricalBinaryTree/SymmetricalBinaryTree.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /Utilities/BinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BinaryTree.h" 3 | 4 | BinaryTreeNode* CreateBinaryTreeNode(int value) 5 | { 6 | BinaryTreeNode* pNode = new BinaryTreeNode(); 7 | pNode->m_nValue = value; 8 | pNode->m_pLeft = NULL; 9 | pNode->m_pRight = NULL; 10 | 11 | return pNode; 12 | } 13 | 14 | void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight) 15 | { 16 | if(pParent != NULL) 17 | { 18 | pParent->m_pLeft = pLeft; 19 | pParent->m_pRight = pRight; 20 | } 21 | } 22 | 23 | void PrintTreeNode(BinaryTreeNode* pNode) 24 | { 25 | if(pNode != NULL) 26 | { 27 | printf("value of this node is: %d\n", pNode->m_nValue); 28 | 29 | if(pNode->m_pLeft != NULL) 30 | printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue); 31 | else 32 | printf("left child is null.\n"); 33 | 34 | if(pNode->m_pRight != NULL) 35 | printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue); 36 | else 37 | printf("right child is null.\n"); 38 | } 39 | else 40 | { 41 | printf("this node is null.\n"); 42 | } 43 | 44 | printf("\n"); 45 | } 46 | 47 | void PrintTree(BinaryTreeNode* pRoot) 48 | { 49 | PrintTreeNode(pRoot); 50 | 51 | if(pRoot != NULL) 52 | { 53 | if(pRoot->m_pLeft != NULL) 54 | PrintTree(pRoot->m_pLeft); 55 | 56 | if(pRoot->m_pRight != NULL) 57 | PrintTree(pRoot->m_pRight); 58 | } 59 | } 60 | 61 | void DestroyTree(BinaryTreeNode* pRoot) 62 | { 63 | if(pRoot != NULL) 64 | { 65 | BinaryTreeNode* pLeft = pRoot->m_pLeft; 66 | BinaryTreeNode* pRight = pRoot->m_pRight; 67 | 68 | delete pRoot; 69 | pRoot = NULL; 70 | 71 | DestroyTree(pLeft); 72 | DestroyTree(pRight); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Utilities/BinaryTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct BinaryTreeNode 4 | { 5 | int m_nValue; 6 | BinaryTreeNode* m_pLeft; 7 | BinaryTreeNode* m_pRight; 8 | }; 9 | 10 | __declspec( dllexport ) BinaryTreeNode* CreateBinaryTreeNode(int value); 11 | __declspec( dllexport ) void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight); 12 | __declspec( dllexport ) void PrintTreeNode(BinaryTreeNode* pNode); 13 | __declspec( dllexport ) void PrintTree(BinaryTreeNode* pRoot); 14 | __declspec( dllexport ) void DestroyTree(BinaryTreeNode* pRoot); 15 | -------------------------------------------------------------------------------- /Utilities/Utilities.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {715E6A55-16B3-4F05-9286-6DBC78DA51E4} 15 | Win32Proj 16 | Utilities 17 | 18 | 19 | 20 | DynamicLibrary 21 | true 22 | v110 23 | Unicode 24 | 25 | 26 | DynamicLibrary 27 | false 28 | v110 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | D:\temp\CodingInterviewBin\ 45 | D:\temp\CodingInterviewBin\$(ProjectName)\ 46 | 47 | 48 | false 49 | 50 | 51 | 52 | 53 | 54 | Level3 55 | Disabled 56 | WIN32;_DEBUG;_WINDOWS;_USRDLL;UTILITIES_EXPORTS;%(PreprocessorDefinitions) 57 | 58 | 59 | Windows 60 | true 61 | 62 | 63 | D:\temp\CodingInterviewBin\$(MSBuildProjectName).log 64 | 65 | 66 | 67 | 68 | Level3 69 | 70 | 71 | MaxSpeed 72 | true 73 | true 74 | WIN32;NDEBUG;_WINDOWS;_USRDLL;UTILITIES_EXPORTS;%(PreprocessorDefinitions) 75 | 76 | 77 | Windows 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /Utilities/Utilities.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | Header Files 31 | 32 | 33 | -------------------------------------------------------------------------------- /Utilities/Utilities.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Utilities/list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | ListNode* CreateListNode(int value) 6 | { 7 | ListNode* pNode = new ListNode(); 8 | pNode->m_nValue = value; 9 | pNode->m_pNext = NULL; 10 | 11 | return pNode; 12 | } 13 | 14 | void ConnectListNodes(ListNode* pCurrent, ListNode* pNext) 15 | { 16 | if(pCurrent == NULL) 17 | { 18 | printf("Error to connect two nodes.\n"); 19 | exit(1); 20 | } 21 | 22 | pCurrent->m_pNext = pNext; 23 | } 24 | 25 | void PrintListNode(ListNode* pNode) 26 | { 27 | if(pNode == NULL) 28 | { 29 | printf("The node is NULL\n"); 30 | } 31 | else 32 | { 33 | printf("The key in node is %d.\n", pNode->m_nValue); 34 | } 35 | } 36 | 37 | void PrintList(ListNode* pHead) 38 | { 39 | printf("PrintList starts.\n"); 40 | 41 | ListNode* pNode = pHead; 42 | while(pNode != NULL) 43 | { 44 | printf("%d\t", pNode->m_nValue); 45 | pNode = pNode->m_pNext; 46 | } 47 | 48 | printf("\nPrintList ends.\n"); 49 | } 50 | 51 | void DestroyList(ListNode* pHead) 52 | { 53 | ListNode* pNode = pHead; 54 | while(pNode != NULL) 55 | { 56 | pHead = pHead->m_pNext; 57 | delete pNode; 58 | pNode = pHead; 59 | } 60 | } 61 | 62 | void AddToTail(ListNode** pHead, int value) 63 | { 64 | ListNode* pNew = new ListNode(); 65 | pNew->m_nValue = value; 66 | pNew->m_pNext = NULL; 67 | 68 | if(*pHead == NULL) 69 | { 70 | *pHead = pNew; 71 | } 72 | else 73 | { 74 | ListNode* pNode = *pHead; 75 | while(pNode->m_pNext != NULL) 76 | pNode = pNode->m_pNext; 77 | 78 | pNode->m_pNext = pNew; 79 | } 80 | } 81 | 82 | void RemoveNode(ListNode** pHead, int value) 83 | { 84 | if(pHead == NULL || *pHead == NULL) 85 | return; 86 | 87 | ListNode* pToBeDeleted = NULL; 88 | if((*pHead)->m_nValue == value) 89 | { 90 | pToBeDeleted = *pHead; 91 | *pHead = (*pHead)->m_pNext; 92 | } 93 | else 94 | { 95 | ListNode* pNode = *pHead; 96 | while(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value) 97 | pNode = pNode->m_pNext; 98 | 99 | if(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue == value) 100 | { 101 | pToBeDeleted = pNode->m_pNext; 102 | pNode->m_pNext = pNode->m_pNext->m_pNext; 103 | } 104 | } 105 | 106 | if(pToBeDeleted != NULL) 107 | { 108 | delete pToBeDeleted; 109 | pToBeDeleted = NULL; 110 | } 111 | } -------------------------------------------------------------------------------- /Utilities/list.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct ListNode 4 | { 5 | int m_nValue; 6 | ListNode* m_pNext; 7 | }; 8 | 9 | __declspec( dllexport ) ListNode* CreateListNode(int value); 10 | __declspec( dllexport ) void ConnectListNodes(ListNode* pCurrent, ListNode* pNext); 11 | __declspec( dllexport ) void PrintListNode(ListNode* pNode); 12 | __declspec( dllexport ) void PrintList(ListNode* pHead); 13 | __declspec( dllexport ) void DestroyList(ListNode* pHead); 14 | __declspec( dllexport ) void AddToTail(ListNode** pHead, int value); 15 | __declspec( dllexport ) void RemoveNode(ListNode** pHead, int value); --------------------------------------------------------------------------------