Skip to content

Commit 0eae07b

Browse files
committed
feat: add solutions to lc problem: No.0662
No.0662.Maximum Width of Binary Tree
1 parent e012801 commit 0eae07b

File tree

10 files changed

+429
-54
lines changed

10 files changed

+429
-54
lines changed

solution/0600-0699/0662.Maximum Width of Binary Tree/README.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,160 @@
7777

7878
<!-- 这里可写通用的实现逻辑 -->
7979

80+
BFS 层序遍历。
81+
8082
<!-- tabs:start -->
8183

8284
### **Python3**
8385

8486
<!-- 这里可写当前语言的特殊实现逻辑 -->
8587

8688
```python
87-
89+
# Definition for a binary tree node.
90+
# class TreeNode:
91+
# def __init__(self, val=0, left=None, right=None):
92+
# self.val = val
93+
# self.left = left
94+
# self.right = right
95+
class Solution:
96+
def widthOfBinaryTree(self, root: TreeNode) -> int:
97+
q = deque([(root, 1)])
98+
ans = 0
99+
while q:
100+
n = len(q)
101+
ans = max(ans, q[-1][1] - q[0][1] + 1)
102+
for _ in range(n):
103+
node, j = q.popleft()
104+
if node.left:
105+
q.append((node.left, 2 * j))
106+
if node.right:
107+
q.append((node.right, 2 * j + 1))
108+
return ans
88109
```
89110

90111
### **Java**
91112

92113
<!-- 这里可写当前语言的特殊实现逻辑 -->
93114

94115
```java
116+
/**
117+
* Definition for a binary tree node.
118+
* public class TreeNode {
119+
* int val;
120+
* TreeNode left;
121+
* TreeNode right;
122+
* TreeNode() {}
123+
* TreeNode(int val) { this.val = val; }
124+
* TreeNode(int val, TreeNode left, TreeNode right) {
125+
* this.val = val;
126+
* this.left = left;
127+
* this.right = right;
128+
* }
129+
* }
130+
*/
131+
class Solution {
132+
public int widthOfBinaryTree(TreeNode root) {
133+
Deque<Pair<TreeNode, Integer>> q = new ArrayDeque<>();
134+
q.offerLast(new Pair<>(root, 1));
135+
int ans = 0;
136+
while (!q.isEmpty()) {
137+
ans = Math.max(ans, q.peekLast().getValue() - q.peekFirst().getValue() + 1);
138+
for (int i = 0, n = q.size(); i < n; ++i) {
139+
Pair<TreeNode, Integer> node = q.pollFirst();
140+
if (node.getKey().left != null) {
141+
q.offerLast(new Pair<>(node.getKey().left, node.getValue() * 2));
142+
}
143+
if (node.getKey().right != null) {
144+
q.offerLast(new Pair<>(node.getKey().right, node.getValue() * 2 + 1));
145+
}
146+
}
147+
}
148+
return ans;
149+
}
150+
}
151+
```
152+
153+
### **C++**
154+
155+
`start * 2` 表示下一层的起点,防止索引溢出。计算下一层左右子树索引时,减去 `start * 2`,可以防止溢出。
156+
157+
```cpp
158+
/**
159+
* Definition for a binary tree node.
160+
* struct TreeNode {
161+
* int val;
162+
* TreeNode *left;
163+
* TreeNode *right;
164+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
165+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
166+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
167+
* };
168+
*/
169+
class Solution {
170+
public:
171+
int widthOfBinaryTree(TreeNode* root) {
172+
queue<pair<TreeNode*, int>> q;
173+
q.emplace(root, 1);
174+
int ans = 0;
175+
while (!q.empty())
176+
{
177+
ans = max(ans, q.back().second - q.front().second + 1);
178+
int start = q.front().second;
179+
for (int i = 0, n = q.size(); i < n; ++i)
180+
{
181+
auto node = q.front();
182+
q.pop();
183+
if (node.first->left != nullptr) q.emplace(node.first->left, node.second * 2 - start * 2);
184+
if (node.first->right != nullptr) q.emplace(node.first->right, node.second * 2 + 1 - start * 2);
185+
}
186+
}
187+
return ans;
188+
}
189+
};
190+
```
95191
192+
### **Go**
193+
194+
```go
195+
/**
196+
* Definition for a binary tree node.
197+
* type TreeNode struct {
198+
* Val int
199+
* Left *TreeNode
200+
* Right *TreeNode
201+
* }
202+
*/
203+
type Node struct {
204+
node *TreeNode
205+
idx int
206+
}
207+
208+
func widthOfBinaryTree(root *TreeNode) int {
209+
q := []Node{{root, 1}}
210+
ans := 0
211+
for len(q) > 0 {
212+
ans = max(ans, q[len(q)-1].idx-q[0].idx+1)
213+
n := len(q)
214+
for i := 0; i < n; i++ {
215+
node := q[0]
216+
q = q[1:]
217+
if node.node.Left != nil {
218+
q = append(q, Node{node.node.Left, node.idx * 2})
219+
}
220+
if node.node.Right != nil {
221+
q = append(q, Node{node.node.Right, node.idx*2 + 1})
222+
}
223+
}
224+
}
225+
return ans
226+
}
227+
228+
func max(a, b int) int {
229+
if a > b {
230+
return a
231+
}
232+
return b
233+
}
96234
```
97235

98236
### **...**

0 commit comments

Comments
 (0)