Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def bstFromPreorder(self, pre: List[int]) -> Optional[TreeNode]:
- i=0
- n=len(pre)
- def dfs(right):
- nonlocal i
- if i==n or pre[i]>right:return None
- root=TreeNode(pre[i])
- i+=1
- root.left=dfs(root.val)
- root.right=dfs(right)
- return root
- return dfs(float('inf'))
Advertisement
Add Comment
Please, Sign In to add comment