Skip to content

Commit 6c4b498

Browse files
Zanger67/leetcodeZanger67/leetcode
authored andcommitted
Updated markdown files
1 parent 186401b commit 6c4b498

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

markdowns/Questions_By_Code_Length.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Calculations are based on the code files's byte sizes.
6363
| 1087 | [Brace Expansion](<https://leetcode.com/problems/brace-expansion>) | Medium | | [solution](<_1087. Brace Expansion.md>) | py | Jun 15, 2024 |
6464
| 606 | [Construct String from Binary Tree](<https://leetcode.com/problems/construct-string-from-binary-tree>) | Medium | | [solution](<_606. Construct String from Binary Tree.md>) | java, py | Aug 04, 2024 |
6565
| 2390 | [Removing Stars From a String](<https://leetcode.com/problems/removing-stars-from-a-string>) | Medium | | [solution](<_2390. Removing Stars From a String.md>) | java, py | Jun 02, 2024 |
66+
| 662 | [Maximum Width of Binary Tree](<https://leetcode.com/problems/maximum-width-of-binary-tree>) | Medium | | [solution](<_662. Maximum Width of Binary Tree.md>) | py | Oct 28, 2024 |
6667
| 1230 | [Toss Strange Coins](<https://leetcode.com/problems/toss-strange-coins>) | Medium | Weekly Premium | [solution](<_1230. Toss Strange Coins.md>) | c, java, py | Jul 29, 2024 |
6768
| 951 | [Flip Equivalent Binary Trees](<https://leetcode.com/problems/flip-equivalent-binary-trees>) | Medium | Daily | [solution](<_951. Flip Equivalent Binary Trees.md>) | py | Oct 24, 2024 |
6869
| 959 | [Regions Cut By Slashes](<https://leetcode.com/problems/regions-cut-by-slashes>) | Medium | Daily | [solution](<_959. Regions Cut By Slashes.md>) | py | Aug 10, 2024 |
@@ -174,7 +175,6 @@ Calculations are based on the code files's byte sizes.
174175
| 1255 | [Maximum Score Words Formed by Letters](<https://leetcode.com/problems/maximum-score-words-formed-by-letters>) | Hard | | [solution](<_1255. Maximum Score Words Formed by Letters.md>) | py | Jun 15, 2024 |
175176
| 708 | [Insert into a Sorted Circular Linked List](<https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list>) | Medium | | [solution](<_708. Insert into a Sorted Circular Linked List.md>) | py | Jun 22, 2024 |
176177
| 3211 | Weekly Contest 405 - q2 - [Generate Binary Strings Without Adjacent Zeros](<https://leetcode.com/problems/generate-binary-strings-without-adjacent-zeros>) | Medium | Contest | [solution](<_3211. Generate Binary Strings Without Adjacent Zeros.md>) | js, py | Jul 07, 2024 |
177-
| 662 | [Maximum Width of Binary Tree](<https://leetcode.com/problems/maximum-width-of-binary-tree>) | Medium | | [solution](<_662. Maximum Width of Binary Tree.md>) | py | Oct 28, 2024 |
178178
| 107 | [Binary Tree Level Order Traversal II](<https://leetcode.com/problems/binary-tree-level-order-traversal-ii>) | Medium | | [solution](<_107. Binary Tree Level Order Traversal II.md>) | java | Jul 04, 2024 |
179179
| 450 | [Delete Node in a BST](<https://leetcode.com/problems/delete-node-in-a-bst>) | Medium | | [solution](<_450. Delete Node in a BST.md>) | py | Jun 29, 2024 |
180180
| 1698 | [Number of Distinct Substrings in a String](<https://leetcode.com/problems/number-of-distinct-substrings-in-a-string>) | Medium | | [solution](<_1698. Number of Distinct Substrings in a String.md>) | java, py | Jun 02, 2024 |

markdowns/_662. Maximum Width of Binary Tree.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,45 @@
2020

2121
## Solutions
2222

23+
- [m662 bfs.py](<../my-submissions/m662 bfs.py>)
2324
- [m662 dfs.py](<../my-submissions/m662 dfs.py>)
2425
### Python
26+
#### [m662 bfs.py](<../my-submissions/m662 bfs.py>)
27+
```Python
28+
# Definition for a binary tree node.
29+
# class TreeNode:
30+
# def __init__(self, val=0, left=None, right=None):
31+
# self.val = val
32+
# self.left = left
33+
# self.right = right
34+
class Solution:
35+
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
36+
nxt_nodes = deque([(root, 0, 0)]) # node, index, depth
37+
38+
max_width = 0
39+
leftmost, rightmost = inf, -inf
40+
curr_depth = None
41+
while nxt_nodes :
42+
curr, indx, depth = nxt_nodes.popleft()
43+
if curr_depth != depth :
44+
max_width = max(max_width, rightmost - leftmost + 1)
45+
leftmost, rightmost, curr_depth = inf, -inf, depth
46+
47+
if leftmost > indx :
48+
leftmost = indx
49+
if rightmost < indx :
50+
rightmost = indx
51+
52+
if curr.left :
53+
nxt_nodes.append((curr.left, 2 * indx, depth + 1))
54+
if curr.right :
55+
nxt_nodes.append((curr.right, 2 * indx + 1, depth + 1))
56+
57+
max_width = max(max_width, rightmost - leftmost + 1)
58+
return max_width
59+
60+
```
61+
2562
#### [m662 dfs.py](<../my-submissions/m662 dfs.py>)
2663
```Python
2764
# Definition for a binary tree node.

0 commit comments

Comments
 (0)