Skip to content

Commit 9f87d70

Browse files
committed
Add 20 more.
1 parent 102def6 commit 9f87d70

22 files changed

+804
-1
lines changed

Balanced_Binary_Tree.txt

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+
};

Binary_Tree_Inorder_Traversal.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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<int> inorderTraversal(TreeNode *root) {
13+
// Start typing your C/C++ solution below
14+
// DO NOT write int main() function
15+
vector<int> ans;
16+
if (root == NULL) {
17+
return ans;
18+
}
19+
stack<TreeNode *> st;
20+
stack<bool> flag;
21+
st.push(root);
22+
flag.push(false);
23+
TreeNode *cntNode = NULL;
24+
bool state;
25+
while (!st.empty()) {
26+
cntNode = st.top();
27+
state = flag.top();
28+
flag.pop();
29+
if (state == false) {
30+
flag.push(true);
31+
if (cntNode->left) {
32+
st.push(cntNode->left);
33+
flag.push(false);
34+
}
35+
} else {
36+
st.pop();
37+
ans.push_back(cntNode->val);
38+
if (cntNode->right) {
39+
st.push(cntNode->right);
40+
flag.push(false);
41+
}
42+
}
43+
}
44+
return ans;
45+
}
46+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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> > levelOrder(TreeNode *root) {
13+
// Start typing your C/C++ solution below
14+
// DO NOT write int main() function
15+
vector<vector<int> > ans;
16+
if (root == NULL) {
17+
return ans;
18+
}
19+
vector<int> tmp;
20+
queue<TreeNode *> q;
21+
queue<int> dep_que;
22+
q.push(root);
23+
dep_que.push(1);
24+
TreeNode *cntNode = NULL;
25+
int cntDepth, minDepth = 1;
26+
while (!q.empty()) {
27+
cntNode = q.front();
28+
q.pop();
29+
cntDepth = dep_que.front();
30+
dep_que.pop();
31+
if (cntDepth > minDepth) {
32+
minDepth = cntDepth;
33+
ans.push_back(tmp);
34+
tmp.clear();
35+
}
36+
tmp.push_back(cntNode->val);
37+
if (cntNode->left) {
38+
q.push(cntNode->left);
39+
dep_que.push(cntDepth + 1);
40+
}
41+
if (cntNode->right) {
42+
q.push(cntNode->right);
43+
dep_que.push(cntDepth + 1);
44+
}
45+
}
46+
if (tmp.size() > 0) {
47+
ans.push_back(tmp);
48+
}
49+
return ans;
50+
}
51+
};
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+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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> > zigzagLevelOrder(TreeNode *root) {
13+
// Start typing your C/C++ solution below
14+
// DO NOT write int main() function
15+
vector<vector<int> > ans;
16+
if (root == NULL) {
17+
return ans;
18+
}
19+
queue<TreeNode *> q;
20+
queue<int> dep_que;
21+
vector<int> tmp;
22+
q.push(root);
23+
dep_que.push(1);
24+
TreeNode *cntNode = NULL;
25+
int cntDepth, minDepth = 1;
26+
bool flag = false;
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+
minDepth = cntDepth;
34+
if (flag) {
35+
reverse(tmp.begin(), tmp.end());
36+
}
37+
flag = !flag;
38+
ans.push_back(tmp);
39+
tmp.clear();
40+
}
41+
tmp.push_back(cntNode->val);
42+
if (cntNode->left) {
43+
q.push(cntNode->left);
44+
dep_que.push(cntDepth + 1);
45+
}
46+
if (cntNode->right) {
47+
q.push(cntNode->right);
48+
dep_que.push(cntDepth + 1);
49+
}
50+
}
51+
if (tmp.size() > 0) {
52+
if (flag) {
53+
reverse(tmp.begin(), tmp.end());
54+
}
55+
ans.push_back(tmp);
56+
}
57+
return ans;
58+
}
59+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
TreeNode *GenTree(vector<int> &inorder, int il, int ir, vector<int> &postorder, int pl, int pr) {
13+
if (il > ir) {
14+
return NULL;
15+
}
16+
TreeNode *root = new TreeNode(postorder[pr]);
17+
int mid = il;
18+
while (inorder[mid] != postorder[pr]) {
19+
mid++;
20+
}
21+
int len = mid - il;
22+
root->left = GenTree(inorder, il, mid - 1, postorder, pl, pl + len - 1);
23+
root->right = GenTree(inorder, mid + 1, ir, postorder, pl + len, pr - 1);
24+
return root;
25+
}
26+
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
27+
// Start typing your C/C++ solution below
28+
// DO NOT write int main() function
29+
if (inorder.size() == 0) {
30+
return NULL;
31+
}
32+
return GenTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
33+
}
34+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
TreeNode *GenTree(vector<int> &preorder, int pl, int pr, vector<int> &inorder, int il, int ir) {
13+
if (pl > pr) {
14+
return NULL;
15+
}
16+
TreeNode *root = new TreeNode(preorder[pl]);
17+
int mid = il;
18+
while (inorder[mid] != preorder[pl]) {
19+
mid++;
20+
}
21+
int len = mid - il;
22+
root->left = GenTree(preorder, pl + 1, pl + len, inorder, il, mid - 1);
23+
root->right = GenTree(preorder, pl + len + 1, pr, inorder, mid + 1, ir);
24+
return root;
25+
}
26+
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
27+
// Start typing your C/C++ solution below
28+
// DO NOT write int main() function
29+
if (preorder.size() == 0) {
30+
return NULL;
31+
}
32+
return GenTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
33+
}
34+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
TreeNode *GenTree(vector<int> &num, int l, int r) {
13+
if (l > r) {
14+
return NULL;
15+
}
16+
int mid = l + (r - l) / 2;
17+
TreeNode *root = new TreeNode(num[mid]);
18+
root->left = GenTree(num, l, mid - 1);
19+
root->right = GenTree(num, mid + 1, r);
20+
return root;
21+
}
22+
TreeNode *sortedArrayToBST(vector<int> &num) {
23+
// Start typing your C/C++ solution below
24+
// DO NOT write int main() function
25+
if (num.size() == 0) {
26+
return NULL;
27+
}
28+
return GenTree(num, 0, num.size() - 1);
29+
}
30+
};

0 commit comments

Comments
 (0)