Skip to content

Commit f964911

Browse files
committed
🚀 25-Aug-2020
1 parent d20fcd2 commit f964911

19 files changed

+1090
-49
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
2+
3+
For example:
4+
Given binary tree [3,9,20,null,null,15,7],
5+
3
6+
/ \
7+
9 20
8+
/ \
9+
15 7
10+
return its level order traversal as:
11+
[
12+
[3],
13+
[9,20],
14+
[15,7]
15+
]
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
/**
28+
* Definition for a binary tree node.
29+
* struct TreeNode {
30+
* int val;
31+
* TreeNode *left;
32+
* TreeNode *right;
33+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
35+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
36+
* };
37+
*/
38+
class Solution {
39+
public:
40+
vector<vector<int>> levelOrder(TreeNode* root) {
41+
vector<vector<int> > res;
42+
if(!root) return res;
43+
queue<TreeNode*> q;
44+
q.push(root);
45+
while(!q.empty()){
46+
int size=q.size();
47+
vector<int> tmp;
48+
while(size--){
49+
TreeNode* curr=q.front();
50+
q.pop();
51+
tmp.push_back(curr->val);
52+
if(curr->left) q.push(curr->left);
53+
if(curr->right) q.push(curr->right);
54+
}
55+
res.push_back(tmp);
56+
}
57+
return res;
58+
}
59+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Given a binary tree, find its maximum depth.
2+
3+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
4+
5+
Note: A leaf is a node with no children.
6+
7+
Example:
8+
9+
Given binary tree [3,9,20,null,null,15,7],
10+
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
return its depth = 3.
17+
18+
19+
20+
21+
22+
23+
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* struct TreeNode {
28+
* int val;
29+
* TreeNode *left;
30+
* TreeNode *right;
31+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
32+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
33+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
34+
* };
35+
*/
36+
class Solution {
37+
public:
38+
int maxDepth(TreeNode* root) {
39+
if(!root) return 0;
40+
return 1 + max(maxDepth(root->left), maxDepth(root->right)) ;
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Given a binary tree, return the vertical order traversal of its nodes values.
2+
3+
For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).
4+
5+
Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).
6+
7+
If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
8+
9+
Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.
10+
11+
12+
13+
Example 1:
14+
15+
16+
17+
Input: [3,9,20,null,null,15,7]
18+
Output: [[9],[3,15],[20],[7]]
19+
Explanation:
20+
Without loss of generality, we can assume the root node is at position (0, 0):
21+
Then, the node with value 9 occurs at position (-1, -1);
22+
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
23+
The node with value 20 occurs at position (1, -1);
24+
The node with value 7 occurs at position (2, -2).
25+
Example 2:
26+
27+
28+
29+
Input: [1,2,3,4,5,6,7]
30+
Output: [[4],[2],[1,5,6],[3],[7]]
31+
Explanation:
32+
The node with value 5 and the node with value 6 have the same position according to the given scheme.
33+
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.
34+
35+
36+
Note:
37+
38+
The tree will have between 1 and 1000 nodes.
39+
Each node's value will be between 0 and 1000.
40+
41+
42+
43+
44+
45+
46+
/**
47+
* Definition for a binary tree node.
48+
* struct TreeNode {
49+
* int val;
50+
* TreeNode *left;
51+
* TreeNode *right;
52+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
53+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
54+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
55+
* };
56+
*/
57+
58+
struct nodes {
59+
int x, y, val;
60+
};
61+
62+
class Solution {
63+
public:
64+
65+
vector<nodes> coords;
66+
67+
static bool comp(nodes a, nodes b){
68+
if(a.x < b.x) return true;
69+
else if(a.x > b.x) return false;
70+
else if(a.y > b.y) return true;
71+
else if(a.y < b.y)return false;
72+
else return a.val < b.val;
73+
}
74+
75+
void traversal(TreeNode* root, int x, int y){
76+
if(!root) return;
77+
coords.push_back({x, y, root->val});
78+
traversal(root->left, x-1, y-1);
79+
traversal(root->right, x+1, y-1);
80+
81+
}
82+
83+
vector<vector<int>> verticalTraversal(TreeNode* root) {
84+
vector<vector<int>> res;
85+
86+
traversal(root, 0, 0); // <root, x coordinate, y coordinate>
87+
88+
sort(coords.begin(), coords.end(), comp);
89+
90+
int n=coords.size(), i=0;
91+
while(i<n){
92+
vector<int> tmp;
93+
do {
94+
tmp.push_back(coords[i].val);
95+
i++;
96+
}while(i<n && coords[i].x==coords[i-1].x);
97+
res.push_back(tmp);
98+
}
99+
100+
101+
return res;
102+
103+
}
104+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
2+
3+
Example:
4+
Given a binary tree
5+
1
6+
/ \
7+
2 3
8+
/ \
9+
4 5
10+
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
11+
12+
Note: The length of path between two nodes is represented by the number of edges between them.
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
/**
24+
* Definition for a binary tree node.
25+
* struct TreeNode {
26+
* int val;
27+
* TreeNode *left;
28+
* TreeNode *right;
29+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
30+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
31+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
32+
* };
33+
*/
34+
class Solution {
35+
public:
36+
37+
int getDiameter(TreeNode* root, int &res){
38+
if(!root) return 0;
39+
40+
int l=getDiameter(root->left, res);
41+
int r=getDiameter(root->right, res);
42+
43+
int tmp=max(l,r)+1;
44+
int ans=max(tmp, l+r+1);
45+
res=max(res, ans);
46+
47+
return tmp;
48+
}
49+
50+
int diameterOfBinaryTree(TreeNode* root) {
51+
if(!root) return 0;
52+
int res=INT_MIN;
53+
getDiameter(root, res);
54+
return res-1;
55+
}
56+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Given a binary tree, return the vertical order traversal of its nodes values.
2+
3+
For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).
4+
5+
Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).
6+
7+
If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
8+
9+
Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.
10+
11+
12+
13+
Example 1:
14+
15+
16+
17+
Input: [3,9,20,null,null,15,7]
18+
Output: [[9],[3,15],[20],[7]]
19+
Explanation:
20+
Without loss of generality, we can assume the root node is at position (0, 0):
21+
Then, the node with value 9 occurs at position (-1, -1);
22+
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
23+
The node with value 20 occurs at position (1, -1);
24+
The node with value 7 occurs at position (2, -2).
25+
Example 2:
26+
27+
28+
29+
Input: [1,2,3,4,5,6,7]
30+
Output: [[4],[2],[1,5,6],[3],[7]]
31+
Explanation:
32+
The node with value 5 and the node with value 6 have the same position according to the given scheme.
33+
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.
34+
35+
36+
Note:
37+
38+
The tree will have between 1 and 1000 nodes.
39+
Each node's value will be between 0 and 1000.
40+
41+
42+
43+
44+
45+
46+
/**
47+
* Definition for a binary tree node.
48+
* struct TreeNode {
49+
* int val;
50+
* TreeNode *left;
51+
* TreeNode *right;
52+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
53+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
54+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
55+
* };
56+
*/
57+
58+
struct nodes {
59+
int x, y, val;
60+
};
61+
62+
class Solution {
63+
public:
64+
65+
vector<nodes> coords;
66+
67+
static bool comp(nodes a, nodes b){
68+
if(a.x < b.x) return true;
69+
else if(a.x > b.x) return false;
70+
else if(a.y > b.y) return true;
71+
else if(a.y < b.y)return false;
72+
else return a.val < b.val;
73+
}
74+
75+
void traversal(TreeNode* root, int x, int y){
76+
if(!root) return;
77+
coords.push_back({x, y, root->val});
78+
traversal(root->left, x-1, y-1);
79+
traversal(root->right, x+1, y-1);
80+
81+
}
82+
83+
vector<vector<int>> verticalTraversal(TreeNode* root) {
84+
vector<vector<int>> res;
85+
86+
traversal(root, 0, 0); // <root, x coordinate, y coordinate>
87+
88+
sort(coords.begin(), coords.end(), comp);
89+
90+
int n=coords.size(), i=0;
91+
while(i<n){
92+
vector<int> tmp;
93+
do {
94+
tmp.push_back(coords[i].val);
95+
i++;
96+
}while(i<n && coords[i].x==coords[i-1].x);
97+
res.push_back(tmp);
98+
}
99+
100+
101+
return res;
102+
103+
}
104+
};

0 commit comments

Comments
 (0)