Skip to content

Commit dd39f87

Browse files
Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
authored and
Zanger67/leetcode
committed
Updated markdown files
1 parent fa1bec9 commit dd39f87

File tree

249 files changed

+15814
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

249 files changed

+15814
-0
lines changed

markdowns/_100. Same Tree.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 100. [Same Tree](<https://leetcode.com/problems/same-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Depth-First Search](<by_topic/Depth-First Search.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **62.636 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [100.same-tree.py](<../my-submissions/100.same-tree.py>)
24+
### Python
25+
#### [100.same-tree.py](<../my-submissions/100.same-tree.py>)
26+
```Python
27+
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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
36+
37+
if not p and not q :
38+
return True
39+
40+
if not p or not q :
41+
return False
42+
43+
if p.val != q.val :
44+
return False
45+
46+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
47+
48+
49+
```
50+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 102. [Binary Tree Level Order Traversal](<https://leetcode.com/problems/binary-tree-level-order-traversal>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **67.774 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [m102.java](<../my-submissions/m102.java>)
24+
### Java
25+
#### [m102.java](<../my-submissions/m102.java>)
26+
```Java
27+
/**
28+
* Definition for a binary tree node.
29+
* public class TreeNode {
30+
* int val;
31+
* TreeNode left;
32+
* TreeNode right;
33+
* TreeNode() {}
34+
* TreeNode(int val) { this.val = val; }
35+
* TreeNode(int val, TreeNode left, TreeNode right) {
36+
* this.val = val;
37+
* this.left = left;
38+
* this.right = right;
39+
* }
40+
* }
41+
*/
42+
class Solution {
43+
public List<List<Integer>> levelOrder(TreeNode root) {
44+
List<List<Integer>> output = new LinkedList<>();
45+
helper(root, 0, output);
46+
return output;
47+
}
48+
49+
private void helper(TreeNode curr, int depth, List<List<Integer>> output) {
50+
if (curr == null) {
51+
return;
52+
}
53+
if (output.size() <= depth) {
54+
output.add(new LinkedList<Integer>());
55+
}
56+
output.get(depth).add(curr.val);
57+
58+
depth++;
59+
if (curr.left != null) {
60+
helper(curr.left, depth, output);
61+
}
62+
if (curr.right != null) {
63+
helper(curr.right, depth, output);
64+
}
65+
}
66+
}
67+
```
68+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 103. [Binary Tree Zigzag Level Order Traversal](<https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **59.391 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [103.binary-tree-zigzag-level-order-traversal.py](<../my-submissions/103.binary-tree-zigzag-level-order-traversal.py>)
24+
- [m103.py](<../my-submissions/m103.py>)
25+
### Python
26+
#### [103.binary-tree-zigzag-level-order-traversal.py](<../my-submissions/103.binary-tree-zigzag-level-order-traversal.py>)
27+
```Python
28+
29+
# Definition for a binary tree node.
30+
# class TreeNode:
31+
# def __init__(self, val=0, left=None, right=None):
32+
# self.val = val
33+
# self.left = left
34+
# self.right = right
35+
class Solution:
36+
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
37+
38+
39+
```
40+
41+
#### [m103.py](<../my-submissions/m103.py>)
42+
```Python
43+
44+
# Definition for a binary tree node.
45+
# class TreeNode:
46+
# def __init__(self, val=0, left=None, right=None):
47+
# self.val = val
48+
# self.left = left
49+
# self.right = right
50+
class Solution:
51+
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
52+
if not root :
53+
return []
54+
toVisit = deque([root])
55+
leftRight = True
56+
output = []
57+
58+
while toVisit :
59+
width = len(toVisit)
60+
lvl = []
61+
for i in range(width):
62+
if leftRight :
63+
curr = toVisit.popleft()
64+
lvl.append(curr.val)
65+
if curr.left :
66+
toVisit.append(curr.left)
67+
if curr.right :
68+
toVisit.append(curr.right)
69+
else :
70+
curr = toVisit.pop()
71+
lvl.append(curr.val)
72+
if curr.right :
73+
toVisit.appendleft(curr.right)
74+
if curr.left :
75+
toVisit.appendleft(curr.left)
76+
77+
output.append(lvl)
78+
leftRight = not leftRight
79+
return output
80+
81+
82+
```
83+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 104. [Maximum Depth of Binary Tree](<https://leetcode.com/problems/maximum-depth-of-binary-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Depth-First Search](<by_topic/Depth-First Search.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **75.683 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [e104.py](<../my-submissions/e104.py>)
24+
### Python
25+
#### [e104.py](<../my-submissions/e104.py>)
26+
```Python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, val=0, left=None, right=None):
30+
# self.val = val
31+
# self.left = left
32+
# self.right = right
33+
class Solution:
34+
def maxDepth(self, root: Optional[TreeNode]) -> int:
35+
if not root :
36+
return 0
37+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
38+
```
39+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 107. [Binary Tree Level Order Traversal II](<https://leetcode.com/problems/binary-tree-level-order-traversal-ii>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Breadth-First Search](<by_topic/Breadth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **63.801 %**
18+
19+
------
20+
21+
> This also could have easily been done by reversing the left-right
22+
> traversal order, then just using the default `Collections` `reverse`
23+
> function to have the deepest layers appear first.
24+
25+
------
26+
27+
## Solutions
28+
29+
- [m107.java](<../my-submissions/m107.java>)
30+
### Java
31+
#### [m107.java](<../my-submissions/m107.java>)
32+
```Java
33+
/**
34+
* Definition for a binary tree node.
35+
* public class TreeNode {
36+
* int val;
37+
* TreeNode left;
38+
* TreeNode right;
39+
* TreeNode() {}
40+
* TreeNode(int val) { this.val = val; }
41+
* TreeNode(int val, TreeNode left, TreeNode right) {
42+
* this.val = val;
43+
* this.left = left;
44+
* this.right = right;
45+
* }
46+
* }
47+
*/
48+
class Solution {
49+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
50+
HashMap<Integer, List<Integer>> levelOrderVals = new HashMap<>();
51+
int maxDepth = helper(root, 0, levelOrderVals);
52+
53+
List<List<Integer>> output = new LinkedList<>();
54+
for (int i = maxDepth; i >= 0; i--) {
55+
output.add(levelOrderVals.get(i));
56+
}
57+
58+
return output;
59+
}
60+
61+
private int helper(TreeNode curr, int depth, HashMap<Integer, List<Integer>> levelOrderVals) {
62+
if (curr == null) {
63+
return depth - 1;
64+
}
65+
if (!levelOrderVals.containsKey(depth)) {
66+
levelOrderVals.put(depth, new LinkedList<Integer>());
67+
}
68+
levelOrderVals.get(depth).add(curr.val);
69+
70+
depth++;
71+
return Integer.max(helper(curr.left, depth, levelOrderVals), helper(curr.right, depth, levelOrderVals));
72+
}
73+
}
74+
```
75+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 110. [Balanced Binary Tree](<https://leetcode.com/problems/balanced-binary-tree>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Tree](<by_topic/Tree.md>), [Depth-First Search](<by_topic/Depth-First Search.md>), [Binary Tree](<by_topic/Binary Tree.md>)**
16+
>
17+
> **Acceptance Rate** : **52.638 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [e110.py](<../my-submissions/e110.py>)
24+
### Python
25+
#### [e110.py](<../my-submissions/e110.py>)
26+
```Python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, val=0, left=None, right=None):
30+
# self.val = val
31+
# self.left = left
32+
# self.right = right
33+
class Solution:
34+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
35+
def dfs(curr: Optional[TreeNode]) -> int :
36+
if not curr :
37+
return 0
38+
39+
left, right = dfs(curr.left), dfs(curr.right)
40+
41+
if left == -1 or right == -1 :
42+
return -1
43+
44+
if abs(left - right) > 1 :
45+
return -1
46+
47+
return max(left, right) + 1
48+
49+
return True if dfs(root) != -1 else False
50+
```
51+

0 commit comments

Comments
 (0)