Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 因为是string,所以这道题主要是注意root.val可能是,负值,或是多位数
- class Solution:
- def buildTree(self, s):
- if len(s) == 0:
- return None
- found = s.find('(')
- if found == -1:
- root = TreeNode(s)
- return root
- else:
- root = TreeNode(s[:found])
- start = found
- st = []
- for i in range(start, len(s)):
- if s[i] == '(':
- st.append('(')
- elif s[i] == ')':
- st.pop()
- if not st and start == found:
- root.left = self.buildTree(s[start+1:i])
- start = i + 1
- elif not st:
- root.right = self.buildTree(s[start+1:i])
- return root
- def str2tree(self, s):
- """
- :type s: str
- :rtype: TreeNode
- """
- root = self.buildTree(s)
- return root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement