-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincreasing_order_st.py
More file actions
42 lines (28 loc) · 1.01 KB
/
increasing_order_st.py
File metadata and controls
42 lines (28 loc) · 1.01 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
# https://leetcode.com/problems/increasing-order-search-tree/submissions/
# 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 increasingBST(self, root: TreeNode) -> TreeNode:
if(root is None):
return root
res = []
self.inorder(root, res)
root.val = res[0]
curr_node = root
for i in range(1, len(res)):
curr_node.left = None
curr_node.right = TreeNode(res[i])
curr_node = curr_node.right
return root
def inorder(self, curr_node, res):
if curr_node is None:
return None
if(curr_node.left):
self.inorder(curr_node.left, res)
res.append(curr_node.val)
if(curr_node.right):
self.inorder(curr_node.right, res)