forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd One Row to Tree.py
39 lines (35 loc) · 933 Bytes
/
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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 Solve(
self,
root,
val,
depth,
curr,
):
if root == None:
return None
if depth == 1:
return TreeNode(val, root)
if curr == depth - 1:
(left, right) = (root.left, root.right)
(root.left, root.right) = (TreeNode(val, left),
TreeNode(val, None, right))
return root
self.Solve(root.left, val, depth, curr + 1)
self.Solve(root.right, val, depth, curr + 1)
return root
def addOneRow(
self,
root,
val,
depth,
):
return self.Solve(root, val, depth, 1)