-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
30 lines (24 loc) · 966 Bytes
/
day20.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
Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def bstFromPreorder(self, preorder):
"""
:type preorder: List[int]
:rtype: TreeNode
"""
inorder = sorted(preorder)
return self.bstFromPreorderandInorder(preorder,inorder)
def bstFromPreorderandInorder(self, preorder, inorder):
lenthpre = len(preorder)
lengthin = len(inorder)
if lengthpre != lengthin or preorder == None or inorder == None or lenthpre == 0:
return None
root = TreeNode(preorder[0])
rootindex = inorder.index(root.val)
root.left = self.bstFromPreorderandInorder(preorder[1:rootindex + 1],inorder[:rootindex])
root.right = self.bstFromPreorderandInorder(preorder[rootindex + 1:],inorder[rootindex + 1:])
return root