-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39_IsBalanceTree.cpp
More file actions
99 lines (87 loc) · 2.72 KB
/
Copy path39_IsBalanceTree.cpp
File metadata and controls
99 lines (87 loc) · 2.72 KB
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
#include <iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
int treeDepth(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return 0;
int leftDepth = treeDepth(pRoot->m_pLeft);
int rightDepth = treeDepth(pRoot->m_pRight);
int depth = (leftDepth > rightDepth) ? leftDepth : rightDepth;
return depth+1;
}
bool isBalanceTree(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return true;
int diff = treeDepth(pRoot->m_pLeft) - treeDepth(pRoot->m_pRight);
if (diff < 0) diff = -diff;
if (diff > 1)
return false;
return isBalanceTree(pRoot->m_pLeft) && isBalanceTree(pRoot->m_pRight);
}
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;
}