Skip to content

Commit 1715975

Browse files
committed
🚀 10-Jul-2020
1 parent c9ebe34 commit 1715975

File tree

3 files changed

+209
-1
lines changed

3 files changed

+209
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
2+
3+
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
4+
5+
Example 1:
6+
7+
Input:
8+
9+
1
10+
/ \
11+
3 2
12+
/ \ \
13+
5 3 9
14+
15+
Output: 4
16+
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
17+
Example 2:
18+
19+
Input:
20+
21+
1
22+
/
23+
3
24+
/ \
25+
5 3
26+
27+
Output: 2
28+
Explanation: The maximum width existing in the third level with the length 2 (5,3).
29+
Example 3:
30+
31+
Input:
32+
33+
1
34+
/ \
35+
3 2
36+
/
37+
5
38+
39+
Output: 2
40+
Explanation: The maximum width existing in the second level with the length 2 (3,2).
41+
Example 4:
42+
43+
Input:
44+
45+
1
46+
/ \
47+
3 2
48+
/ \
49+
5 9
50+
/ \
51+
6 7
52+
Output: 8
53+
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
54+
55+
56+
Note: Answer will in the range of 32-bit signed integer.
57+
58+
59+
60+
61+
62+
63+
64+
65+
/**
66+
* Definition for a binary tree node.
67+
* struct TreeNode {
68+
* int val;
69+
* TreeNode *left;
70+
* TreeNode *right;
71+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
72+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
73+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
74+
* };
75+
*/
76+
class Solution {
77+
public:
78+
int widthOfBinaryTree(TreeNode* root) {
79+
if(!root) return 0;
80+
int max_w=0;
81+
deque<TreeNode*> dq;
82+
dq.push_back(root);
83+
while(!dq.empty()){
84+
while(!dq.empty() && dq.front()==NULL) dq.pop_front();
85+
while(!dq.empty() && dq.back()==NULL) dq.pop_back();
86+
int size=dq.size();
87+
if(size > max_w) max_w=size;
88+
while(size--){
89+
TreeNode* tmp=dq.front();
90+
dq.pop_front();
91+
if(tmp){
92+
dq.push_back(tmp->left);
93+
dq.push_back(tmp->right);
94+
} else {
95+
dq.push_back(NULL);
96+
dq.push_back(NULL);
97+
}
98+
99+
}
100+
101+
}
102+
return max_w;
103+
}
104+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
2+
3+
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
4+
5+
Example 1:
6+
7+
Input:
8+
9+
1
10+
/ \
11+
3 2
12+
/ \ \
13+
5 3 9
14+
15+
Output: 4
16+
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
17+
Example 2:
18+
19+
Input:
20+
21+
1
22+
/
23+
3
24+
/ \
25+
5 3
26+
27+
Output: 2
28+
Explanation: The maximum width existing in the third level with the length 2 (5,3).
29+
Example 3:
30+
31+
Input:
32+
33+
1
34+
/ \
35+
3 2
36+
/
37+
5
38+
39+
Output: 2
40+
Explanation: The maximum width existing in the second level with the length 2 (3,2).
41+
Example 4:
42+
43+
Input:
44+
45+
1
46+
/ \
47+
3 2
48+
/ \
49+
5 9
50+
/ \
51+
6 7
52+
Output: 8
53+
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
54+
55+
56+
Note: Answer will in the range of 32-bit signed integer.
57+
58+
59+
60+
61+
62+
63+
64+
65+
/**
66+
* Definition for a binary tree node.
67+
* struct TreeNode {
68+
* int val;
69+
* TreeNode *left;
70+
* TreeNode *right;
71+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
72+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
73+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
74+
* };
75+
*/
76+
class Solution {
77+
public:
78+
int widthOfBinaryTree(TreeNode* root) {
79+
if(!root) return 0;
80+
int max_w=0;
81+
deque<TreeNode*> dq;
82+
dq.push_back(root);
83+
while(!dq.empty()){
84+
while(!dq.empty() && dq.front()==NULL) dq.pop_front();
85+
while(!dq.empty() && dq.back()==NULL) dq.pop_back();
86+
int size=dq.size();
87+
if(size > max_w) max_w=size;
88+
while(size--){
89+
TreeNode* tmp=dq.front();
90+
dq.pop_front();
91+
if(tmp){
92+
dq.push_back(tmp->left);
93+
dq.push_back(tmp->right);
94+
} else {
95+
dq.push_back(NULL);
96+
dq.push_back(NULL);
97+
}
98+
99+
}
100+
101+
}
102+
return max_w;
103+
}
104+
};

graph/dfs.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void adjListGraph(vector<int> adj[], int s, int d){
1010
void dfs(vector<int> adj[], int s, bool vis[]){
1111
vis[s]=true;
1212
cout<<s<<" ";
13-
for(auto x : adj[d]){
13+
for(auto x : adj[s]){
1414
if(!vis[x]){
1515
dfs(adj, x, vis);
1616
}

0 commit comments

Comments
 (0)