-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-one-row-to-tree.py
40 lines (33 loc) · 1.19 KB
/
add-one-row-to-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
newRoot = TreeNode(val)
newRoot.left = root
return newRoot
qu = deque()
qu.append(root)
while qu:
depth -= 1
if depth == 1:
l = len(qu)
for _ in range(l):
curr = qu.popleft()
currLeft, currRight = curr.left, curr.right
newLeft, newRight = TreeNode(val), TreeNode(val)
curr.left, curr.right = newLeft, newRight
newLeft.left, newRight.right = currLeft, currRight
else:
l = len(qu)
for _ in range(l):
curr = qu.popleft()
if curr.left:
qu.append(curr.left)
if curr.right:
qu.append(curr.right)
return root