Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 624c52f

Browse files
committedJul 4, 2024
Grinded level order questions and additional tree questions
1 parent bbc7753 commit 624c52f

16 files changed

+539
-4
lines changed
 

‎README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ Profile: [Zanger](https://leetcode.com/u/Zanger/)
6363
| 94 | [Binary Tree Inorder Traversal](<https://leetcode.com/problems/binary-tree-inorder-traversal>) | Easy | | [solution](<markdowns/_94. Binary Tree Inorder Traversal.md>) | java, py |
6464
| 98 | [Validate Binary Search Tree](<https://leetcode.com/problems/validate-binary-search-tree>) | Medium | B75, N150 | [solution](<markdowns/_98. Validate Binary Search Tree.md>) | c |
6565
| 100 | [Same Tree](<https://leetcode.com/problems/same-tree>) | Easy | B75, N150 | [solution](<markdowns/_100. Same Tree.md>) | py |
66+
| 102 | [Binary Tree Level Order Traversal](<https://leetcode.com/problems/binary-tree-level-order-traversal>) | Medium | B75, N150 | [solution](<markdowns/_102. Binary Tree Level Order Traversal.md>) | java |
6667
| 103 | [Binary Tree Zigzag Level Order Traversal](<https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal>) | Medium | | [solution](<markdowns/_103. Binary Tree Zigzag Level Order Traversal.md>) | py |
67-
| 111 | [Minimum Depth of Binary Tree](<https://leetcode.com/problems/minimum-depth-of-binary-tree>) | Easy | | [solution](<markdowns/_111. Minimum Depth of Binary Tree.md>) | c |
68+
| 107 | [Binary Tree Level Order Traversal II](<https://leetcode.com/problems/binary-tree-level-order-traversal-ii>) | Medium | | [solution](<markdowns/_107. Binary Tree Level Order Traversal II.md>) | java |
69+
| 111 | [Minimum Depth of Binary Tree](<https://leetcode.com/problems/minimum-depth-of-binary-tree>) | Easy | | [solution](<markdowns/_111. Minimum Depth of Binary Tree.md>) | java, c |
6870
| 112 | [Path Sum](<https://leetcode.com/problems/path-sum>) | Easy | | [solution](<markdowns/_112. Path Sum.md>) | py |
6971
| 113 | [Path Sum II](<https://leetcode.com/problems/path-sum-ii>) | Medium | | [solution](<markdowns/_113. Path Sum II.md>) | py |
7072
| 114 | [Flatten Binary Tree to Linked List](<https://leetcode.com/problems/flatten-binary-tree-to-linked-list>) | Medium | | [solution](<markdowns/_114. Flatten Binary Tree to Linked List.md>) | py, c |
@@ -139,6 +141,7 @@ Profile: [Zanger](https://leetcode.com/u/Zanger/)
139141
| 414 | [Third Maximum Number](<https://leetcode.com/problems/third-maximum-number>) | Easy | | [solution](<markdowns/_414. Third Maximum Number.md>) | py |
140142
| 419 | [Battleships in a Board](<https://leetcode.com/problems/battleships-in-a-board>) | Medium | | [solution](<markdowns/_419. Battleships in a Board.md>) | py, c |
141143
| 426 | [Convert Binary Search Tree to Sorted Doubly Linked List](<https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list>) | Medium | | [solution](<markdowns/_426. Convert Binary Search Tree to Sorted Doubly Linked List.md>) | py |
144+
| 429 | [N-ary Tree Level Order Traversal](<https://leetcode.com/problems/n-ary-tree-level-order-traversal>) | Medium | | [solution](<markdowns/_429. N-ary Tree Level Order Traversal.md>) | py |
142145
| 430 | [Flatten a Multilevel Doubly Linked List](<https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list>) | Medium | | [solution](<markdowns/_430. Flatten a Multilevel Doubly Linked List.md>) | java, cpp |
143146
| 431 | [Encode N-ary Tree to Binary Tree](<https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree>) | Hard | | [solution](<markdowns/_431. Encode N-ary Tree to Binary Tree.md>) | java, py |
144147
| 437 | [Path Sum III](<https://leetcode.com/problems/path-sum-iii>) | Medium | | [solution](<markdowns/_437. Path Sum III.md>) | py |
@@ -163,6 +166,7 @@ Profile: [Zanger](https://leetcode.com/u/Zanger/)
163166
| 611 | [Valid Triangle Number](<https://leetcode.com/problems/valid-triangle-number>) | Medium | | [solution](<markdowns/_611. Valid Triangle Number.md>) | py |
164167
| 616 | [Add Bold Tag in String](<https://leetcode.com/problems/add-bold-tag-in-string>) | Medium | | [solution](<markdowns/_616. Add Bold Tag in String.md>) | py |
165168
| 619 | [Biggest Single Number](<https://leetcode.com/problems/biggest-single-number>) | Easy | | [solution](<markdowns/_619. Biggest Single Number.md>) | sql |
169+
| 637 | [Average of Levels in Binary Tree](<https://leetcode.com/problems/average-of-levels-in-binary-tree>) | Easy | | [solution](<markdowns/_637. Average of Levels in Binary Tree.md>) | py |
166170
| 645 | [Set Mismatch](<https://leetcode.com/problems/set-mismatch>) | Easy | | [solution](<markdowns/_645. Set Mismatch.md>) | java, py |
167171
| 648 | [Replace Words](<https://leetcode.com/problems/replace-words>) | Medium | Daily | [solution](<markdowns/_648. Replace Words.md>) | java, py |
168172
| 651 | [4 Keys Keyboard](<https://leetcode.com/problems/4-keys-keyboard>) | Medium | | [solution](<markdowns/_651. 4 Keys Keyboard.md>) | py |
@@ -208,6 +212,7 @@ Profile: [Zanger](https://leetcode.com/u/Zanger/)
208212
| 974 | [Subarray Sums Divisible by K](<https://leetcode.com/problems/subarray-sums-divisible-by-k>) | Medium | Daily | [solution](<markdowns/_974. Subarray Sums Divisible by K.md>) | java, py |
209213
| 977 | [Squares of a Sorted Array](<https://leetcode.com/problems/squares-of-a-sorted-array>) | Easy | | [solution](<markdowns/_977. Squares of a Sorted Array.md>) | py |
210214
| 987 | [Vertical Order Traversal of a Binary Tree](<https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree>) | Hard | | [solution](<markdowns/_987. Vertical Order Traversal of a Binary Tree.md>) | py |
215+
| 993 | [Cousins in Binary Tree](<https://leetcode.com/problems/cousins-in-binary-tree>) | Easy | | [solution](<markdowns/_993. Cousins in Binary Tree.md>) | py |
211216
| 994 | [Rotting Oranges](<https://leetcode.com/problems/rotting-oranges>) | Medium | N150 | [solution](<markdowns/_994. Rotting Oranges.md>) | py |
212217
| 995 | [Minimum Number of K Consecutive Bit Flips](<https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips>) | Hard | Daily | [solution](<markdowns/_995. Minimum Number of K Consecutive Bit Flips.md>) | py, c |
213218
| 1002 | [Find Common Characters](<https://leetcode.com/problems/find-common-characters>) | Easy | Daily | [solution](<markdowns/_1002. Find Common Characters.md>) | py |

‎markdowns/Questions_By_Code_Length.md

Lines changed: 6 additions & 1 deletion
Large diffs are not rendered by default.

‎markdowns/Questions_By_Recent.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Calculations are based on the date of the first solve.
44

55
| # | Title | Level | Cats | Solution | Languages | Date Complete |
66
|-----:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------|:---------------|:---------------------------------------------------------------------------------------------------|:---------------------------------|:----------------|
7+
| 993 | [Cousins in Binary Tree](<https://leetcode.com/problems/cousins-in-binary-tree>) | Easy | | [solution](<_993. Cousins in Binary Tree.md>) | py | Jul 03, 2024 |
8+
| 429 | [N-ary Tree Level Order Traversal](<https://leetcode.com/problems/n-ary-tree-level-order-traversal>) | Medium | | [solution](<_429. N-ary Tree Level Order Traversal.md>) | py | Jul 03, 2024 |
9+
| 637 | [Average of Levels in Binary Tree](<https://leetcode.com/problems/average-of-levels-in-binary-tree>) | Easy | | [solution](<_637. Average of Levels in Binary Tree.md>) | py | Jul 03, 2024 |
10+
| 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 03, 2024 |
11+
| 102 | [Binary Tree Level Order Traversal](<https://leetcode.com/problems/binary-tree-level-order-traversal>) | Medium | B75, N150 | [solution](<_102. Binary Tree Level Order Traversal.md>) | java | Jul 03, 2024 |
712
| 2181 | [Merge Nodes in Between Zeros](<https://leetcode.com/problems/merge-nodes-in-between-zeros>) | Medium | Daily | [solution](<_2181. Merge Nodes in Between Zeros.md>) | java, cpp, c, py | Jul 03, 2024 |
813
| 687 | [Longest Univalue Path](<https://leetcode.com/problems/longest-univalue-path>) | Medium | | [solution](<_687. Longest Univalue Path.md>) | py | Jul 03, 2024 |
914
| 437 | [Path Sum III](<https://leetcode.com/problems/path-sum-iii>) | Medium | | [solution](<_437. Path Sum III.md>) | py | Jul 03, 2024 |
@@ -247,7 +252,7 @@ Calculations are based on the date of the first solve.
247252
| 1783 | [Grand Slam Titles](<https://leetcode.com/problems/grand-slam-titles>) | Medium | | [solution](<_1783. Grand Slam Titles.md>) | sql | Jun 07, 2024 |
248253
| 129 | [Sum Root to Leaf Numbers](<https://leetcode.com/problems/sum-root-to-leaf-numbers>) | Medium | | [solution](<_129. Sum Root to Leaf Numbers.md>) | py | Jun 07, 2024 |
249254
| 69 | [Sqrt(x)](<https://leetcode.com/problems/sqrtx>) | Easy | | [solution](<_69. Sqrt(x).md>) | py | Jun 07, 2024 |
250-
| 111 | [Minimum Depth of Binary Tree](<https://leetcode.com/problems/minimum-depth-of-binary-tree>) | Easy | | [solution](<_111. Minimum Depth of Binary Tree.md>) | c | Jun 07, 2024 |
255+
| 111 | [Minimum Depth of Binary Tree](<https://leetcode.com/problems/minimum-depth-of-binary-tree>) | Easy | | [solution](<_111. Minimum Depth of Binary Tree.md>) | java, c | Jun 07, 2024 |
251256
| 125 | [Valid Palindrome](<https://leetcode.com/problems/valid-palindrome>) | Easy | B75, N150 | [solution](<_125. Valid Palindrome.md>) | py, c | Jun 07, 2024 |
252257
| 196 | [Delete Duplicate Emails](<https://leetcode.com/problems/delete-duplicate-emails>) | Easy | | [solution](<_196. Delete Duplicate Emails.md>) | sql, py | Jun 07, 2024 |
253258
| 2482 | [Difference Between Ones and Zeros in Row and Column](<https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column>) | Medium | | [solution](<_2482. Difference Between Ones and Zeros in Row and Column.md>) | py | Jun 07, 2024 |
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 102. [Binary Tree Level Order Traversal](<https://leetcode.com/problems/binary-tree-level-order-traversal>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Tree, Breadth-First Search, Binary Tree
11+
12+
**AC %:** 67.528
13+
14+
15+
## Solutions
16+
17+
- [m102.java](<../my-submissions/m102.java>)
18+
### Java
19+
#### [m102.java](<../my-submissions/m102.java>)
20+
```Java
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* int val;
25+
* TreeNode left;
26+
* TreeNode right;
27+
* TreeNode() {}
28+
* TreeNode(int val) { this.val = val; }
29+
* TreeNode(int val, TreeNode left, TreeNode right) {
30+
* this.val = val;
31+
* this.left = left;
32+
* this.right = right;
33+
* }
34+
* }
35+
*/
36+
class Solution {
37+
public List<List<Integer>> levelOrder(TreeNode root) {
38+
List<List<Integer>> output = new LinkedList<>();
39+
helper(root, 0, output);
40+
return output;
41+
}
42+
43+
private void helper(TreeNode curr, int depth, List<List<Integer>> output) {
44+
if (curr == null) {
45+
return;
46+
}
47+
if (output.size() <= depth) {
48+
output.add(new LinkedList<Integer>());
49+
}
50+
output.get(depth).add(curr.val);
51+
52+
depth++;
53+
if (curr.left != null) {
54+
helper(curr.left, depth, output);
55+
}
56+
if (curr.right != null) {
57+
helper(curr.right, depth, output);
58+
}
59+
}
60+
}
61+
```
62+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 107. [Binary Tree Level Order Traversal II](<https://leetcode.com/problems/binary-tree-level-order-traversal-ii>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Tree, Breadth-First Search, Binary Tree
11+
12+
**AC %:** 63.637
13+
14+
15+
------
16+
This also could have easily been done by reversing the left-right traversal order, then just using the default `Collections` `reverse` function to have the deepest layers appear first.
17+
18+
## Solutions
19+
20+
- [m107.java](<../my-submissions/m107.java>)
21+
### Java
22+
#### [m107.java](<../my-submissions/m107.java>)
23+
```Java
24+
/**
25+
* Definition for a binary tree node.
26+
* public class TreeNode {
27+
* int val;
28+
* TreeNode left;
29+
* TreeNode right;
30+
* TreeNode() {}
31+
* TreeNode(int val) { this.val = val; }
32+
* TreeNode(int val, TreeNode left, TreeNode right) {
33+
* this.val = val;
34+
* this.left = left;
35+
* this.right = right;
36+
* }
37+
* }
38+
*/
39+
class Solution {
40+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
41+
HashMap<Integer, List<Integer>> levelOrderVals = new HashMap<>();
42+
int maxDepth = helper(root, 0, levelOrderVals);
43+
44+
List<List<Integer>> output = new LinkedList<>();
45+
for (int i = maxDepth; i >= 0; i--) {
46+
output.add(levelOrderVals.get(i));
47+
}
48+
49+
return output;
50+
}
51+
52+
private int helper(TreeNode curr, int depth, HashMap<Integer, List<Integer>> levelOrderVals) {
53+
if (curr == null) {
54+
return depth - 1;
55+
}
56+
if (!levelOrderVals.containsKey(depth)) {
57+
levelOrderVals.put(depth, new LinkedList<Integer>());
58+
}
59+
levelOrderVals.get(depth).add(curr.val);
60+
61+
depth++;
62+
return Integer.max(helper(curr.left, depth, levelOrderVals), helper(curr.right, depth, levelOrderVals));
63+
}
64+
}
65+
```
66+

‎markdowns/_111. Minimum Depth of Binary Tree.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
*First completed: June 07, 2024*
44

5-
*Last updated: July 01, 2024*
5+
*Last updated: July 03, 2024*
66

77

88
> *To see the question prompt, click the title.*
@@ -15,6 +15,7 @@
1515
## Solutions
1616

1717
- [e111.c](<../my-submissions/e111.c>)
18+
- [e111.java](<../my-submissions/e111.java>)
1819
### C
1920
#### [e111.c](<../my-submissions/e111.c>)
2021
```C
@@ -50,3 +51,46 @@ int minDepth(struct TreeNode* root) {
5051

5152
```
5253
54+
### Java
55+
#### [e111.java](<../my-submissions/e111.java>)
56+
```Java
57+
/**
58+
* Definition for a binary tree node.
59+
* public class TreeNode {
60+
* int val;
61+
* TreeNode left;
62+
* TreeNode right;
63+
* TreeNode() {}
64+
* TreeNode(int val) { this.val = val; }
65+
* TreeNode(int val, TreeNode left, TreeNode right) {
66+
* this.val = val;
67+
* this.left = left;
68+
* this.right = right;
69+
* }
70+
* }
71+
*/
72+
class Solution {
73+
public int minDepth(TreeNode root) {
74+
if (root == null) {
75+
return 0;
76+
}
77+
return minDepth(root, 1);
78+
}
79+
private int minDepth(TreeNode curr, int depth) {
80+
if (curr.left == null && curr.right == null) {
81+
return depth;
82+
}
83+
84+
depth++;
85+
if (curr.left != null && curr.right != null) {
86+
return Integer.min(minDepth(curr.left, depth), minDepth(curr.right, depth));
87+
}
88+
89+
if (curr.left != null) {
90+
return minDepth(curr.left, depth);
91+
}
92+
return minDepth(curr.right, depth);
93+
}
94+
}
95+
```
96+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 429. [N-ary Tree Level Order Traversal](<https://leetcode.com/problems/n-ary-tree-level-order-traversal>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Tree, Breadth-First Search
11+
12+
**AC %:** 70.85
13+
14+
15+
## Solutions
16+
17+
- [m429.py](<../my-submissions/m429.py>)
18+
### Python
19+
#### [m429.py](<../my-submissions/m429.py>)
20+
```Python
21+
"""
22+
# Definition for a Node.
23+
class Node:
24+
def __init__(self, val=None, children=None):
25+
self.val = val
26+
self.children = children
27+
"""
28+
29+
class Solution:
30+
def levelOrder(self, root: 'Node') -> List[List[int]]:
31+
output = []
32+
33+
def dfs(curr: 'Node', depth: int = 0) -> None :
34+
if not curr :
35+
return
36+
if depth >= len(output) :
37+
output.append([])
38+
39+
output[depth].append(curr.val)
40+
41+
depth += 1
42+
for child in curr.children :
43+
dfs(child, depth)
44+
45+
dfs(root)
46+
return output
47+
```
48+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 637. [Average of Levels in Binary Tree](<https://leetcode.com/problems/average-of-levels-in-binary-tree>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Tree, Depth-First Search, Breadth-First Search, Binary Tree
11+
12+
**AC %:** 72.799
13+
14+
15+
## Solutions
16+
17+
- [e637.py](<../my-submissions/e637.py>)
18+
### Python
19+
#### [e637.py](<../my-submissions/e637.py>)
20+
```Python
21+
# Definition for a binary tree node.
22+
# class TreeNode:
23+
# def __init__(self, val=0, left=None, right=None):
24+
# self.val = val
25+
# self.left = left
26+
# self.right = right
27+
class Solution:
28+
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
29+
hs = defaultdict(list)
30+
31+
def dfs(curr: Optional[TreeNode], depth: int = 0) -> None :
32+
if not curr :
33+
return
34+
35+
hs[depth].append(curr.val)
36+
37+
depth += 1
38+
dfs(curr.left, depth)
39+
dfs(curr.right, depth)
40+
41+
dfs(root, 0)
42+
print(hs)
43+
return [sum(hs[x]) / len(hs[x]) for x in range(1 + max(hs.keys()))]
44+
```
45+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 993. [Cousins in Binary Tree](<https://leetcode.com/problems/cousins-in-binary-tree>)
2+
3+
*First completed: July 03, 2024*
4+
5+
*Last updated: July 03, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Tree, Depth-First Search, Breadth-First Search, Binary Tree
11+
12+
**AC %:** 56.207
13+
14+
15+
## Solutions
16+
17+
- [e993.py](<../my-submissions/e993.py>)
18+
### Python
19+
#### [e993.py](<../my-submissions/e993.py>)
20+
```Python
21+
# Definition for a binary tree node.
22+
# class TreeNode:
23+
# def __init__(self, val=0, left=None, right=None):
24+
# self.val = val
25+
# self.left = left
26+
# self.right = right
27+
class Solution:
28+
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
29+
# schema: (depth, parent)
30+
ref = []
31+
32+
def dfs(curr: Optional[TreeNode], prev: Optional[TreeNode], depth: int = 0) -> None :
33+
if not curr or len(ref) >= 2 :
34+
return
35+
36+
if ref and depth > ref[0][0] :
37+
return
38+
39+
if curr.val == x :
40+
ref.append((depth, prev))
41+
elif curr.val == y :
42+
ref.append((depth, prev))
43+
44+
depth += 1
45+
dfs(curr.left, curr, depth)
46+
dfs(curr.right, curr, depth)
47+
48+
dfs(root, None)
49+
50+
if len(ref) < 2 :
51+
return False
52+
return ref[0][0] == ref[1][0] and ref[0][1] != ref[1][1]
53+
```
54+

‎my-submissions/e111.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int minDepth(TreeNode root) {
18+
if (root == null) {
19+
return 0;
20+
}
21+
return minDepth(root, 1);
22+
}
23+
private int minDepth(TreeNode curr, int depth) {
24+
if (curr.left == null && curr.right == null) {
25+
return depth;
26+
}
27+
28+
depth++;
29+
if (curr.left != null && curr.right != null) {
30+
return Integer.min(minDepth(curr.left, depth), minDepth(curr.right, depth));
31+
}
32+
33+
if (curr.left != null) {
34+
return minDepth(curr.left, depth);
35+
}
36+
return minDepth(curr.right, depth);
37+
}
38+
}

‎my-submissions/e637.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
9+
hs = defaultdict(list)
10+
11+
def dfs(curr: Optional[TreeNode], depth: int = 0) -> None :
12+
if not curr :
13+
return
14+
15+
hs[depth].append(curr.val)
16+
17+
depth += 1
18+
dfs(curr.left, depth)
19+
dfs(curr.right, depth)
20+
21+
dfs(root, 0)
22+
print(hs)
23+
return [sum(hs[x]) / len(hs[x]) for x in range(1 + max(hs.keys()))]

‎my-submissions/e993.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
9+
# schema: (depth, parent)
10+
ref = []
11+
12+
def dfs(curr: Optional[TreeNode], prev: Optional[TreeNode], depth: int = 0) -> None :
13+
if not curr or len(ref) >= 2 :
14+
return
15+
16+
if ref and depth > ref[0][0] :
17+
return
18+
19+
if curr.val == x :
20+
ref.append((depth, prev))
21+
elif curr.val == y :
22+
ref.append((depth, prev))
23+
24+
depth += 1
25+
dfs(curr.left, curr, depth)
26+
dfs(curr.right, curr, depth)
27+
28+
dfs(root, None)
29+
30+
if len(ref) < 2 :
31+
return False
32+
return ref[0][0] == ref[1][0] and ref[0][1] != ref[1][1]

‎my-submissions/m102.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<List<Integer>> levelOrder(TreeNode root) {
18+
List<List<Integer>> output = new LinkedList<>();
19+
helper(root, 0, output);
20+
return output;
21+
}
22+
23+
private void helper(TreeNode curr, int depth, List<List<Integer>> output) {
24+
if (curr == null) {
25+
return;
26+
}
27+
if (output.size() <= depth) {
28+
output.add(new LinkedList<Integer>());
29+
}
30+
output.get(depth).add(curr.val);
31+
32+
depth++;
33+
if (curr.left != null) {
34+
helper(curr.left, depth, output);
35+
}
36+
if (curr.right != null) {
37+
helper(curr.right, depth, output);
38+
}
39+
}
40+
}

‎my-submissions/m107.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
18+
HashMap<Integer, List<Integer>> levelOrderVals = new HashMap<>();
19+
int maxDepth = helper(root, 0, levelOrderVals);
20+
21+
List<List<Integer>> output = new LinkedList<>();
22+
for (int i = maxDepth; i >= 0; i--) {
23+
output.add(levelOrderVals.get(i));
24+
}
25+
26+
return output;
27+
}
28+
29+
private int helper(TreeNode curr, int depth, HashMap<Integer, List<Integer>> levelOrderVals) {
30+
if (curr == null) {
31+
return depth - 1;
32+
}
33+
if (!levelOrderVals.containsKey(depth)) {
34+
levelOrderVals.put(depth, new LinkedList<Integer>());
35+
}
36+
levelOrderVals.get(depth).add(curr.val);
37+
38+
depth++;
39+
return Integer.max(helper(curr.left, depth, levelOrderVals), helper(curr.right, depth, levelOrderVals));
40+
}
41+
}

‎my-submissions/m107.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This also could have easily been done by reversing the left-right traversal order, then just using the default `Collections` `reverse` function to have the deepest layers appear first.

‎my-submissions/m429.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, children=None):
5+
self.val = val
6+
self.children = children
7+
"""
8+
9+
class Solution:
10+
def levelOrder(self, root: 'Node') -> List[List[int]]:
11+
output = []
12+
13+
def dfs(curr: 'Node', depth: int = 0) -> None :
14+
if not curr :
15+
return
16+
if depth >= len(output) :
17+
output.append([])
18+
19+
output[depth].append(curr.val)
20+
21+
depth += 1
22+
for child in curr.children :
23+
dfs(child, depth)
24+
25+
dfs(root)
26+
return output

0 commit comments

Comments
 (0)
Please sign in to comment.