码迷,mamicode.com
首页 > 其他好文 > 详细

JZ-C-39-Plus

时间:2016-06-22 21:50:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

剑指offer第三十九题-拓展:输入一棵二叉树的根结点,判断该树是不是平衡二叉树(AVL)

  1 //============================================================================
  2 // Name        : JZ-C-39-Plus.cpp
  3 // Author      : Laughing_Lz
  4 // Version     :
  5 // Copyright   : All Right Reserved
  6 // Description : 39-拓展:输入一棵二叉树的根结点,判断该树是不是平衡二叉树(AVL)
  7 //============================================================================
  8 
  9 #include <iostream>
 10 #include <stdio.h>
 11 #include "BinaryTree.h"
 12 using namespace std;
 13 
 14 // ====================方法1====================
 15 /**
 16  * 代码简洁,但是由于一个结点会被重复遍历多次,所以效率不高
 17  */
 18 int TreeDepth(BinaryTreeNode* pRoot) {
 19     if (pRoot == NULL)
 20         return 0;
 21 
 22     int nLeft = TreeDepth(pRoot->m_pLeft);
 23     int nRight = TreeDepth(pRoot->m_pRight);
 24 
 25     return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
 26 }
 27 
 28 bool IsBalanced_Solution1(BinaryTreeNode* pRoot) {
 29     if (pRoot == NULL)
 30         return true;
 31 
 32     int left = TreeDepth(pRoot->m_pLeft);
 33     int right = TreeDepth(pRoot->m_pRight);
 34     int diff = left - right;
 35     if (diff > 1 || diff < -1)
 36         return false;
 37 
 38     return IsBalanced_Solution1(pRoot->m_pLeft)
 39             && IsBalanced_Solution1(pRoot->m_pRight);
 40 }
 41 
 42 // ====================方法2====================
 43 /**
 44  * 采用后序遍历★方式遍历二叉树的每一个结点,在遍历到一个结点之前就已经遍历了它的左右子树。
 45  * 一边遍历一边判断每个结点是不是平衡的
 46  */
 47 bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth);
 48 
 49 bool IsBalanced_Solution2(BinaryTreeNode* pRoot) {
 50     int depth = 0;
 51     return IsBalanced(pRoot, &depth);
 52 }
 53 
 54 bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth) {
 55     if (pRoot == NULL) {
 56         *pDepth = 0;
 57         return true;
 58     }
 59 
 60     int left, right;
 61     if (IsBalanced(pRoot->m_pLeft, &left)
 62             && IsBalanced(pRoot->m_pRight, &right)) {
 63         int diff = left - right;
 64         if (diff <= 1 && diff >= -1) {
 65             *pDepth = 1 + (left > right ? left : right);
 66             return true;
 67         }
 68     }
 69 
 70     return false;
 71 }
 72 
 73 // ====================测试代码====================
 74 void Test(char* testName, BinaryTreeNode* pRoot, bool expected) {
 75     if (testName != NULL)
 76         printf("%s begins:\n", testName);
 77 
 78     printf("Solution1 begins: ");
 79     if (IsBalanced_Solution1(pRoot) == expected)
 80         printf("Passed.\n");
 81     else
 82         printf("Failed.\n");
 83 
 84     printf("Solution2 begins: ");
 85     if (IsBalanced_Solution2(pRoot) == expected)
 86         printf("Passed.\n");
 87     else
 88         printf("Failed.\n");
 89 }
 90 
 91 // 完全二叉树
 92 //             1
 93 //         /       94 //        2        3
 95 //       /\       /  96 //      4  5     6   7
 97 void Test1() {
 98     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
 99     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
100     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
101     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
102     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
103     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
104     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
105 
106     ConnectTreeNodes(pNode1, pNode2, pNode3);
107     ConnectTreeNodes(pNode2, pNode4, pNode5);
108     ConnectTreeNodes(pNode3, pNode6, pNode7);
109 
110     Test("Test1", pNode1, true);
111 
112     DestroyTree(pNode1);
113 }
114 
115 // 不是完全二叉树,但是平衡二叉树
116 //             1
117 //         /      118 //        2        3
119 //       /\         120 //      4  5         6
121 //        /
122 //       7
123 void Test2() {
124     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
125     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
126     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
127     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
128     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
129     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
130     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
131 
132     ConnectTreeNodes(pNode1, pNode2, pNode3);
133     ConnectTreeNodes(pNode2, pNode4, pNode5);
134     ConnectTreeNodes(pNode3, NULL, pNode6);
135     ConnectTreeNodes(pNode5, pNode7, NULL);
136 
137     Test("Test2", pNode1, true);
138 
139     DestroyTree(pNode1);
140 }
141 
142 // 不是平衡二叉树
143 //             1
144 //         /      145 //        2        3
146 //       /147 //      4  5
148 //        /
149 //       6
150 void Test3() {
151     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
152     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
153     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
154     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
155     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
156     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
157 
158     ConnectTreeNodes(pNode1, pNode2, pNode3);
159     ConnectTreeNodes(pNode2, pNode4, pNode5);
160     ConnectTreeNodes(pNode5, pNode6, NULL);
161 
162     Test("Test3", pNode1, false);
163 
164     DestroyTree(pNode1);
165 }
166 
167 //               1
168 //              /
169 //             2
170 //            /
171 //           3
172 //          /
173 //         4
174 //        /
175 //       5
176 void Test4() {
177     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
178     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
179     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
180     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
181     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
182 
183     ConnectTreeNodes(pNode1, pNode2, NULL);
184     ConnectTreeNodes(pNode2, pNode3, NULL);
185     ConnectTreeNodes(pNode3, pNode4, NULL);
186     ConnectTreeNodes(pNode4, pNode5, NULL);
187 
188     Test("Test4", pNode1, false);
189 
190     DestroyTree(pNode1);
191 }
192 
193 // 1
194 //  195 //   2
196 //    197 //     3
198 //      199 //       4
200 //        201 //         5
202 void Test5() {
203     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
204     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
205     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
206     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
207     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
208 
209     ConnectTreeNodes(pNode1, NULL, pNode2);
210     ConnectTreeNodes(pNode2, NULL, pNode3);
211     ConnectTreeNodes(pNode3, NULL, pNode4);
212     ConnectTreeNodes(pNode4, NULL, pNode5);
213 
214     Test("Test5", pNode1, false);
215 
216     DestroyTree(pNode1);
217 }
218 
219 // 树中只有1个结点
220 void Test6() {
221     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
222     Test("Test6", pNode1, true);
223 
224     DestroyTree(pNode1);
225 }
226 
227 // 树中没有结点
228 void Test7() {
229     Test("Test7", NULL, true);
230 }
231 
232 int main(int argc, char** argv) {
233     Test1();
234     Test2();
235     Test3();
236     Test4();
237     Test5();
238     Test6();
239     Test7();
240 
241     return 0;
242 }

 

JZ-C-39-Plus

标签:

原文地址:http://www.cnblogs.com/Laughing-Lz/p/5608553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!