-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path623.py
More file actions
52 lines (35 loc) · 1.62 KB
/
623.py
File metadata and controls
52 lines (35 loc) · 1.62 KB
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
"""
623. Add One Row to Tree
Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the
given depth depth.
Note that the root node is at depth 1.
The adding rule is:
* Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with
value val as cur's left subtree root and right subtree root.
* current's original left subtree should be the left subtree of the new left subtree root.
* current's original right subtree should be the right subtree of the new right subtree root.
*If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
"""
# Depth First Approach
class Solution:
def addRowHelper(self, tree_depth, root, val, depth):
if not root:
return None
if tree_depth == depth - 1:
left_node = root.left
right_node = root.right
root.left = TreeNode(val=val)
root.right = TreeNode(val=val)
root.left.left = left_node
root.right.right = right_node
return root
root.left = self.addRowHelper(tree_depth + 1, root.left, val, depth)
root.right = self.addRowHelper(tree_depth + 1, root.right, val, depth)
return root
def addOneRow(self, root, val, depth):
tree_depth = 1
if depth == 1:
new_node = TreeNode(val=val)
new_node.left = root
return new_node
return self.addRowHelper(tree_depth, root, val, depth)