forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0919.py
41 lines (36 loc) · 976 Bytes
/
0919.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
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class CBTInserter:
def __init__(self, root):
"""
:type root: TreeNode
"""
self.data = list()
self.data.append(root)
for node in self.data:
if node.left:
self.data.append(node.left)
if node.right:
self.data.append(node.right)
def insert(self, v):
"""
:type v: int
:rtype: int
"""
parent = self.data[(len(self.data) - 1)//2]
if parent.left:
parent.right = TreeNode(v)
self.data += [parent.right]
else:
parent.left = TreeNode(v)
self.data += [parent.left]
return parent.val
def get_root(self):
"""
:rtype: TreeNode
"""
return self.data[0]