Guest User

Untitled

a guest
Feb 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. # Time: O(N) where n the number of nodes in the input tree, as each node is checked once.
  2. # Space: O(N)
  3. '''
  4.  
  5. V = 5
  6. 4
  7. / \x
  8. 2 6
  9. x/ \
  10. 5 7
  11.  
  12. 4
  13. / \
  14. 2 5
  15.  
  16. 6
  17. \
  18. 7
  19.  
  20. 1) if root.val <= 4, continue to split root.right (left, right )
  21. 1. root.right = left
  22. 2. return root, right
  23. 2) else: continue to split root.left
  24. 1. by the same logic
  25. 2.
  26.  
  27. '''
  28.  
  29.  
  30.  
  31. # Definition for a binary tree node.
  32. # class TreeNode(object):
  33. # def __init__(self, x):
  34. # self.val = x
  35. # self.left = None
  36. # self.right = None
  37.  
  38.  
  39. class Solution(object):
  40. def splitBST(self, root, V):
  41. """
  42. :type root: TreeNode
  43. :type V: int
  44. :rtype: List[TreeNode]
  45. """
  46. if not root:
  47. return None, None
  48. elif root.val <= V:
  49. left, right = self.splitBST(root.right, V)
  50. root.right = left
  51. return root, right
  52. else:
  53. left, right = self.splitBST(root.left, V)
  54. root.left = right
  55. return left, root
Add Comment
Please, Sign In to add comment