Skip to content

Commit eb6d9ab

Browse files
authored
Merge pull request gzc426#284 from hopewolf/master
new version
2 parents fbdab49 + fca7600 commit eb6d9ab

File tree

7 files changed

+277
-0
lines changed

7 files changed

+277
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
leetcode of 101
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isSymmetric(TreeNode* p1,TreeNode * p2){
15+
if((p1 == nullptr && p2!=nullptr) || (p2 == nullptr && p1!=nullptr)) return false;
16+
if(p1 == nullptr && p2 == nullptr) return true;
17+
if(p1->val != p2->val) return false;
18+
return isSymmetric(p1->left,p2->right) && isSymmetric(p1->right,p2->left);
19+
20+
}
21+
bool isSymmetric(TreeNode* root) {
22+
return isSymmetric(root,root);
23+
}
24+
};
25+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
leetcode of 102
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
struct DataNode{
13+
TreeNode * p;
14+
int depth;
15+
};
16+
class Solution {
17+
public:
18+
vector<vector<int>> levelOrder(TreeNode* root) {
19+
vector<vector<int>> ans;
20+
if(root == nullptr) return ans;
21+
queue<DataNode> q;
22+
int depth = 0;
23+
q.push(DataNode{root,depth});
24+
vector<int>t;
25+
while(!q.empty()){
26+
DataNode dn = q.front();
27+
q.pop();
28+
if(depth == dn.depth){
29+
t.push_back(dn.p->val);
30+
}else{
31+
depth = dn.depth;
32+
ans.push_back(t);
33+
t.clear();
34+
t.push_back(dn.p->val);
35+
}
36+
if(dn.p->left != nullptr)
37+
q.push(DataNode{dn.p->left,depth+1});
38+
if(dn.p->right != nullptr)
39+
q.push(DataNode{dn.p->right,depth+1});
40+
41+
}
42+
ans.push_back(t);
43+
return ans;
44+
45+
}
46+
};
47+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
leetcode of 103
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
struct DN{
13+
TreeNode * p;
14+
int depth;
15+
};
16+
class Solution {
17+
public:
18+
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
19+
vector<vector<int>> ans;
20+
if(root == nullptr) return ans;
21+
bool flag = true;
22+
int depth = 0;
23+
queue<DN> q;
24+
vector<int>t;
25+
q.push(DN{root,0});
26+
while(!q.empty()){
27+
DN dn = q.front();
28+
q.pop();
29+
if(dn.depth == depth){
30+
t.push_back(dn.p->val);
31+
}else{
32+
depth = dn.depth;
33+
if(!flag) reverse(t.begin(),t.end());
34+
ans.push_back(t);
35+
flag=!flag;
36+
t.clear();
37+
t.push_back(dn.p->val);
38+
}
39+
40+
if(dn.p->left) q.push(DN{dn.p->left,depth+1});
41+
if(dn.p->right) q.push(DN{dn.p->right,depth+1});
42+
43+
if(q.empty())
44+
{
45+
if(!flag) reverse(t.begin(),t.end());
46+
ans.push_back(t);
47+
}
48+
}
49+
50+
return ans;
51+
52+
53+
}
54+
};
55+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
leetcode of 104
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int maxDepth(TreeNode* root) {
15+
if(root == nullptr){
16+
return 0;
17+
}
18+
int l = maxDepth(root->left);
19+
int r = maxDepth(root->right);
20+
int m = l > r ? l : r;
21+
return m+1;
22+
}
23+
};
24+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
leetcode of 105
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
15+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder,int s,int t,int &i) {
16+
if(i == preorder.size()) return nullptr;
17+
int val = preorder[i++];
18+
TreeNode * root = new TreeNode(val);
19+
if(s >= t - 1){
20+
return root;
21+
}
22+
int index = find(inorder.begin()+s,inorder.begin()+t,val) - inorder.begin();
23+
if(index != s){
24+
root->left = buildTree(preorder,inorder,s,index,i);
25+
}
26+
if(index != t - 1){
27+
root->right = buildTree(preorder,inorder,index+1,t,i);
28+
}
29+
return root;
30+
31+
32+
}
33+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder){
34+
int i=0;
35+
return buildTree(preorder,inorder,0,preorder.size(),i);
36+
}
37+
};
38+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
leetcode of 106
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode * buildTree(vector<int>& inorder, vector<int>& postorder,int s,int t,int & i) {
15+
if(s == t) return nullptr;
16+
TreeNode * root = new TreeNode(postorder[i--]);
17+
int val = root->val;
18+
if(s >= t-1) return root;
19+
int index = find(inorder.begin()+s,inorder.begin()+t,val) - inorder.begin();
20+
if(index != t-1){
21+
root->right = buildTree(inorder,postorder,index+1,t,i);
22+
}
23+
if(index != s){
24+
root->left = buildTree(inorder,postorder,s,index,i);
25+
}
26+
27+
return root;
28+
29+
}
30+
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
31+
int i = inorder.size()-1;
32+
return buildTree(inorder,postorder,0,inorder.size(),i);
33+
}
34+
};
35+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
leetcode of 107
2+
```
3+
/**
4+
* Definition for a binary tree node.
5+
* struct TreeNode {
6+
* int val;
7+
* TreeNode *left;
8+
* TreeNode *right;
9+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10+
* };
11+
*/
12+
struct DNODE{
13+
int depth;
14+
TreeNode * p;
15+
DNODE(int d,TreeNode * p){
16+
this->depth = d;
17+
this->p = p;
18+
}
19+
};
20+
class Solution {
21+
public:
22+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
23+
queue<DNODE> q;
24+
DNODE dn(0,root);
25+
q.push(dn);
26+
int depth = 0;
27+
vector<vector<int>>ans;
28+
if(root == nullptr) return ans;
29+
vector<int>cur;
30+
while(!q.empty()){
31+
dn=q.front();
32+
q.pop();
33+
if(dn.p->left){
34+
q.push(DNODE(dn.depth+1,dn.p->left));
35+
}
36+
if(dn.p->right){
37+
q.push(DNODE(dn.depth+1,dn.p->right));
38+
}
39+
if(depth == dn.depth){
40+
cur.push_back(dn.p->val);
41+
}else if(depth != dn.depth){
42+
ans.push_back(cur);
43+
vector<int>().swap(cur);
44+
cur.push_back(dn.p->val);
45+
depth = dn.depth;
46+
}
47+
}
48+
ans.push_back(cur);
49+
reverse(ans.begin(),ans.end());
50+
return ans;
51+
}
52+
};
53+
```

0 commit comments

Comments
 (0)