JZ-C-39
剑指offer第三十九题:二叉树的深度:输入一棵二叉树的根结点,求该树的深度
1 //============================================================================ 2 // Name : JZ-C-39.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 二叉树的深度:输入一棵二叉树的根结点,求该树的深度 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include "BinaryTree.h" 12 using namespace std; 13 /** 14 * 这是前序遍历吗?(不是吧,后序遍历,以计算路径和) 15 */ 16 int TreeDepth(BinaryTreeNode* pRoot) { 17 if (pRoot == NULL) 18 return 0; 19 20 int nLeft = TreeDepth(pRoot->m_pLeft); 21 int nRight = TreeDepth(pRoot->m_pRight); 22 23 return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1); 24 } 25 26 // ====================测试代码==================== 27 void Test(BinaryTreeNode* pRoot, int expected) { 28 int result = TreeDepth(pRoot); 29 if (expected == result) 30 printf("Test passed.\n"); 31 else 32 printf("Test failed.\n"); 33 } 34 35 // 1 36 // / \ 37 // 2 3 38 // /\ \ 39 // 4 5 6 40 // / 41 // 7 42 void Test1() { 43 printf("Test1 begins.\n"); 44 45 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 46 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 47 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 48 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 49 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 50 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 51 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 52 53 ConnectTreeNodes(pNode1, pNode2, pNode3); 54 ConnectTreeNodes(pNode2, pNode4, pNode5); 55 ConnectTreeNodes(pNode3, NULL, pNode6); 56 ConnectTreeNodes(pNode5, pNode7, NULL); 57 58 Test(pNode1, 4); 59 60 DestroyTree(pNode1); 61 } 62 63 // 1 64 // / 65 // 2 66 // / 67 // 3 68 // / 69 // 4 70 // / 71 // 5 72 void Test2() { 73 printf("Test2 begins.\n"); 74 75 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 76 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 77 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 78 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 79 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 80 81 ConnectTreeNodes(pNode1, pNode2, NULL); 82 ConnectTreeNodes(pNode2, pNode3, NULL); 83 ConnectTreeNodes(pNode3, pNode4, NULL); 84 ConnectTreeNodes(pNode4, pNode5, NULL); 85 86 Test(pNode1, 5); 87 88 DestroyTree(pNode1); 89 } 90 91 // 1 92 // \ 93 // 2 94 // \ 95 // 3 96 // \ 97 // 4 98 // \ 99 // 5 100 void Test3() { 101 printf("Test3 begins.\n"); 102 103 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 104 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 105 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 106 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 107 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 108 109 ConnectTreeNodes(pNode1, NULL, pNode2); 110 ConnectTreeNodes(pNode2, NULL, pNode3); 111 ConnectTreeNodes(pNode3, NULL, pNode4); 112 ConnectTreeNodes(pNode4, NULL, pNode5); 113 114 Test(pNode1, 5); 115 116 DestroyTree(pNode1); 117 } 118 119 // 树中只有1个结点 120 void Test4() { 121 printf("Test4 begins.\n"); 122 123 BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); 124 Test(pNode1, 1); 125 126 DestroyTree(pNode1); 127 } 128 129 // 树中没有结点 130 void Test5() { 131 printf("Test5 begins.\n"); 132 133 Test(NULL, 0); 134 } 135 136 int main(int argc, char** argv) { 137 Test1(); 138 Test2(); 139 Test3(); 140 Test4(); 141 Test5(); 142 143 return 0; 144 }
—————————————————————————————————————行走在人猿的并行线——Laughing_Lz