-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path897.py
57 lines (53 loc) · 1.71 KB
/
897.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
56
57
__________________________________________________________________________________________________
sample 76 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTree(self, root: TreeNode, arr: List[TreeNode]):
if root is None:
return
if(root.left):
self.inorderTree(root.left, arr)
arr.append(root)
if(root.right):
self.inorderTree(root.right, arr)
def increasingBST(self, root: TreeNode) -> TreeNode:
if root is None:
return root
arr = []
self.inorderTree(root, arr)
length = len(arr)
root = arr[0]
cur = root
for i in range(1, length):
cur.left = None
cur.right = arr[i]
cur = arr[i]
cur.left = None
cur.right = None
return root
__________________________________________________________________________________________________
sample 13168 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def increasingBST(self, root):
def inorder(node):
if node:
inorder(node.left)
node.left = None
self.cur.right = node
self.cur = node
inorder(node.right)
ans = self.cur = TreeNode(None)
inorder(root)
return ans.right
__________________________________________________________________________________________________