Skip to content

Commit 36d297b

Browse files
committed
Modify suffix.
1 parent 9f87d70 commit 36d297b

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Balanced_Binary_Tree.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for binary tree
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool judge(TreeNode *root, int &depth) {
13+
if (root == NULL) {
14+
depth = 0;
15+
return true;
16+
}
17+
int leftHeight, rightHeight;
18+
if (judge(root->left, leftHeight) && judge(root->right, rightHeight)) {
19+
if (leftHeight > rightHeight + 1 || rightHeight > leftHeight + 1)
20+
return false;
21+
if (leftHeight > rightHeight) {
22+
depth = leftHeight + 1;
23+
} else {
24+
depth = rightHeight + 1;
25+
}
26+
return true;
27+
}
28+
return false;
29+
}
30+
bool isBalanced(TreeNode *root) {
31+
// Start typing your C/C++ solution below
32+
// DO NOT write int main() function
33+
if (root == NULL) {
34+
return true;
35+
}
36+
int tmp = 0;
37+
return judge(root, tmp);
38+
}
39+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Definition for binary tree
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
vector<vector<int> > levelOrderBottom(TreeNode *root) {
13+
// Start typing your C/C++ solution below
14+
// DO NOT write int main() function
15+
stack<vector<int> > st;
16+
vector<vector<int> > ans;
17+
if (root == NULL) {
18+
return ans;
19+
}
20+
vector<int> tmp;
21+
queue<TreeNode *> q;
22+
queue<int> dep_que;
23+
TreeNode *cntNode;
24+
int minDepth = 1, cntDepth;
25+
q.push(root);
26+
dep_que.push(1);
27+
while (!q.empty()) {
28+
cntNode = q.front();
29+
q.pop();
30+
cntDepth = dep_que.front();
31+
dep_que.pop();
32+
if (cntDepth > minDepth) {
33+
st.push(tmp);
34+
tmp.clear();
35+
minDepth = cntDepth;
36+
}
37+
tmp.push_back(cntNode->val);
38+
if (cntNode->left) {
39+
q.push(cntNode->left);
40+
dep_que.push(cntDepth + 1);
41+
}
42+
if (cntNode->right) {
43+
q.push(cntNode->right);
44+
dep_que.push(cntDepth + 1);
45+
}
46+
}
47+
if (tmp.size() > 0) {
48+
ans.push_back(tmp);
49+
}
50+
while (!st.empty()) {
51+
ans.push_back(st.top());
52+
st.pop();
53+
}
54+
return ans;
55+
}
56+
};

0 commit comments

Comments
 (0)