-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39_IsBalanceTree2.cpp
105 lines (92 loc) · 2.8 KB
/
39_IsBalanceTree2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
bool isBalanceTree(BinaryTreeNode* pRoot, int& depth)
{
if (pRoot == NULL)
{
depth = 0;
return true;
}
int leftDepth = -1;
bool left = isBalanceTree(pRoot->m_pLeft, leftDepth);
int rightDepth = -1;
bool right = isBalanceTree(pRoot->m_pRight, rightDepth);
depth = (leftDepth > rightDepth) ? (leftDepth+1):(rightDepth+1);
int diff = leftDepth - rightDepth;
if ((diff > 1) || (diff < -1))
return false;
return left && right;
}
bool isBalanceTree(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return true;
int depth = -1;
return isBalanceTree(pRoot, depth);
}
static void test(const char* testName, BinaryTreeNode* pRoot, bool expected)
{
cout << testName << " Begins: ";
bool result = isBalanceTree(pRoot);
if (result == expected)
cout << "Passed." << endl;
else
cout << "Failed." << endl;
}
static void test1()
{
BinaryTreeNode* pRoot;
pRoot = new BinaryTreeNode;
pRoot->m_nValue = 1;
pRoot->m_pLeft = new BinaryTreeNode;
pRoot->m_pLeft->m_nValue = 2;
pRoot->m_pRight = new BinaryTreeNode;
pRoot->m_pRight->m_nValue = 3;
pRoot->m_pLeft->m_pLeft = new BinaryTreeNode;
pRoot->m_pLeft->m_pLeft->m_nValue = 4;
pRoot->m_pRight->m_pLeft = NULL;
pRoot->m_pLeft->m_pRight = new BinaryTreeNode;
pRoot->m_pLeft->m_pRight->m_nValue = 5;
pRoot->m_pRight->m_pRight = new BinaryTreeNode;
pRoot->m_pRight->m_pRight->m_nValue = 6;
pRoot->m_pRight->m_pRight->m_pLeft = NULL;
pRoot->m_pRight->m_pRight->m_pRight = NULL;
pRoot->m_pLeft->m_pRight->m_pLeft = new BinaryTreeNode;
pRoot->m_pLeft->m_pRight->m_pLeft->m_nValue = 7;
pRoot->m_pLeft->m_pRight->m_pRight = NULL;
pRoot->m_pLeft->m_pRight->m_pLeft->m_pLeft = NULL;
pRoot->m_pLeft->m_pRight->m_pLeft->m_pRight = NULL;
test("test1", pRoot, true);
}
static void test2()
{
BinaryTreeNode* pRoot = new BinaryTreeNode;
pRoot->m_nValue = 1;
pRoot->m_pLeft = new BinaryTreeNode;
pRoot->m_pLeft->m_nValue = 2;
pRoot->m_pRight = new BinaryTreeNode;
pRoot->m_pRight->m_nValue = 5;
pRoot->m_pRight->m_pLeft = NULL;
pRoot->m_pRight->m_pRight = NULL;
pRoot->m_pLeft->m_pLeft = new BinaryTreeNode;
pRoot->m_pLeft->m_pLeft->m_nValue = 3;
pRoot->m_pLeft->m_pRight = NULL;
pRoot->m_pLeft->m_pLeft->m_pLeft = NULL;
pRoot->m_pLeft->m_pLeft->m_pRight = new BinaryTreeNode;
pRoot->m_pLeft->m_pLeft->m_pRight->m_nValue = 4;
pRoot->m_pLeft->m_pLeft->m_pRight->m_pLeft = NULL;
pRoot->m_pLeft->m_pLeft->m_pRight->m_pRight = NULL;
test("test2", pRoot, false);
}
int main()
{
test1();
test2();
return 0;
}