-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApr-16-24.py
33 lines (33 loc) · 1.17 KB
/
Apr-16-24.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
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
curr = TreeNode(val)
curr.left = root
return curr
def preOrder(root,d,depth,val):
if root:
d += 1
if d==depth:
print(root.val)
tempLeft = root.left
tempRight = root.right
if root.left!=None:
l = TreeNode(val)
root.left = l
l.left = tempLeft
else:
root.left = TreeNode(val)
if root.right!=None:
r = TreeNode(val)
root.right = r
r.right = tempRight
else:
root.right = TreeNode(val)
return
if root.left:
preOrder(root.left,d,depth,val)
if root.right:
preOrder(root.right,d,depth,val)
return
preOrder(root,1,depth,val)
return root