-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.cpp
51 lines (44 loc) · 1.15 KB
/
balance.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
vector<int> vector;
if(!root)
return vector;
stack<TreeNode *> stack;
stack.push(root);
int tl,tr;
while(!stack.empty())
{
tl=0,tr=0;
TreeNode *pNode = stack.top();
// cout<<"pnode="<<pNode->val;
if(pNode->left->left!=NULL){
tl=2;
}
else if(pNode->left!=NULL){
tl=1;
}
else if(pNode->left==NULL){
tl=0;
}
if(pNode->right->right!=NULL){
tr=2;
}
else if(pNode->right!=NULL){
tr=1;
}
else if(pNode->right==NULL){
tr=0;
}
if(abs(tr-tl)>1){
return 0;
}
stack.pop();
if(pNode->right)
{
stack.push(pNode->right);
pNode->right = NULL;
}
if(pNode->left){
stack.push(pNode->left);
pNode->left = NULL;
}
}
return 1;