Skip to content

Commit 9b2b165

Browse files
committed
solved binary tree
1 parent cad9d05 commit 9b2b165

File tree

9 files changed

+268
-2
lines changed

9 files changed

+268
-2
lines changed

Easy/112.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
14+
15+
def helper(node, curr_sum):
16+
if not node:
17+
return False
18+
19+
curr_sum += node.val
20+
21+
# If it's a leaf, check if the sum matches
22+
if not node.left and not node.right:
23+
return curr_sum == targetSum
24+
25+
# Otherwise, explore both sides
26+
return helper(node.left, curr_sum) or helper(node.right, curr_sum)
27+
28+
return helper(root, 0)

Easy/222.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def countNodes(self, root: Optional[TreeNode]) -> int:
14+
if not root:
15+
return 0
16+
left = self.countLeft(root)
17+
right = self.countRight(root)
18+
19+
if left == right:
20+
return 2**left - 1
21+
else:
22+
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
23+
24+
def countLeft(self, node):
25+
h = 0
26+
while node:
27+
h += 1
28+
node = node.left
29+
return h
30+
31+
def countRight(self, node):
32+
h = 0
33+
while node:
34+
h += 1
35+
node = node.right
36+
return h

Medium/106.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import deque
2+
from typing import List, Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
14+
# Map each value to its index in inorder for O(1) lookups
15+
mapping = {val: i for i, val in enumerate(inorder)}
16+
17+
postorder = deque(postorder)
18+
19+
def build(start, end):
20+
if start > end:
21+
return None
22+
23+
# The last element in postorder is the root
24+
root_val = postorder.pop()
25+
root = TreeNode(root_val)
26+
27+
# Find the index of the root in inorder
28+
idx = mapping[root_val]
29+
30+
# Build right subtree first (because of postorder's order)
31+
root.right = build(idx + 1, end)
32+
root.left = build(start, idx - 1)
33+
34+
return root
35+
36+
return build(0, len(inorder) - 1)

Medium/114.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Optional
2+
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
class Solution:
12+
def flatten(self, root: Optional[TreeNode]) -> None:
13+
"""
14+
Do not return anything, modify root in-place instead.
15+
"""
16+
curr = root
17+
while curr:
18+
if curr.left:
19+
# Find the rightmost node in left subtree
20+
rightmost = curr.left
21+
while rightmost.right:
22+
rightmost = rightmost.right
23+
24+
# Connect right subtree to rightmost node
25+
rightmost.right = curr.right
26+
27+
# Move left subtree to the right
28+
curr.right = curr.left
29+
curr.left = None
30+
31+
# Move to next right node
32+
curr = curr.right

Medium/117.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Definition for a Node.
2+
from collections import deque
3+
4+
5+
class Node:
6+
def __init__(
7+
self,
8+
val: int = 0,
9+
left: "Node" = None,
10+
right: "Node" = None,
11+
next: "Node" = None,
12+
):
13+
self.val = val
14+
self.left = left
15+
self.right = right
16+
self.next = next
17+
18+
19+
class Solution:
20+
def connect(self, root: "Node") -> "Node":
21+
22+
q = deque()
23+
q.append(root)
24+
25+
while q:
26+
length = len(q)
27+
prev = None
28+
for i in range(length):
29+
node = q.popleft()
30+
if not node:
31+
continue
32+
if node.left:
33+
q.append(node.left)
34+
if node.right:
35+
q.append(node.right)
36+
if prev:
37+
prev.next = node
38+
prev = node
39+
if prev:
40+
prev.next = None
41+
return root

Medium/129.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def sumNumbers(self, root: Optional[TreeNode]) -> int:
14+
summm = 0
15+
16+
def helper(node, summ):
17+
nonlocal summm
18+
if not node:
19+
return
20+
21+
if not node.right and not node.left:
22+
summm += summ * 10 + node.val
23+
24+
helper(node.left, summ * 10 + node.val)
25+
helper(node.right, summ * 10 + node.val)
26+
27+
helper(root, 0)
28+
return summm

Medium/173.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class BSTIterator:
13+
14+
def __init__(self, root):
15+
self.stack = []
16+
while root:
17+
self.stack.append(root)
18+
root = root.left
19+
20+
def next(self):
21+
node = self.stack.pop()
22+
r = node.right
23+
while r:
24+
self.stack.append(r)
25+
r = r.left
26+
return node.val
27+
28+
def hasNext(self):
29+
return len(self.stack) > 0
30+
31+
32+
# Your BSTIterator object will be instantiated and called as such:
33+
# obj = BSTIterator(root)
34+
# param_1 = obj.next()
35+
# param_2 = obj.hasNext()

Medium/236.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
9+
class Solution:
10+
def lowestCommonAncestor(
11+
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
12+
) -> "TreeNode":
13+
ans = None
14+
15+
def helper(node):
16+
nonlocal ans
17+
if not node:
18+
return False
19+
foundFirst = helper(node.left)
20+
foundSecond = helper(node.right)
21+
22+
if (foundFirst and foundSecond) or (
23+
(foundFirst or foundSecond) and (node == p or node == q)
24+
):
25+
ans = node
26+
return True
27+
return foundFirst or foundSecond or (node == p or node == q)
28+
29+
helper(root)
30+
return ans

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ This repository contains my solutions to various LeetCode problems. Each solutio
1717

1818
### Easy Problems
1919

20-
- **Total Solved:** [48]
20+
- **Total Solved:** [50]
2121

2222
### Medium Problems
2323

24-
- **Total Solved:** [122]
24+
- **Total Solved:** [128]
2525

2626
### Hard Problems
2727

0 commit comments

Comments
 (0)