-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0623-add-one-row-to-tree.py
56 lines (39 loc) · 1.65 KB
/
0623-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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:
return TreeNode(val, root, None)
queue = [root]
while 0 < len(queue):
depth -= 1
count = len(queue)
for _ in range(count):
node = queue.pop(0)
if node.left is not None:
queue.append(node.left)
if node.right is not None:
queue.append(node.right)
if depth == 1:
node.left = TreeNode(val, node.left, None)
node.right = TreeNode(val, None, node.right)
if depth == 1:
break
return root
# class Solution:
# def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
# if depth == 1:
# return TreeNode(val, root, None)
# elif depth == 2:
# root.left = TreeNode(val, root.left, None)
# root.right = TreeNode(val, None, root.right)
# else:
# if root.left:
# self.addOneRow(root.left, val, depth - 1)
# if root.right:
# self.addOneRow(root.right, val, depth - 1)
# return root