From 360c4995f96f88031cb4d4176518dd358bf6410f Mon Sep 17 00:00:00 2001 From: kamyu Date: Thu, 8 Feb 2018 21:42:45 +0800 Subject: [PATCH] Create split-bst.py --- Python/split-bst.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Python/split-bst.py diff --git a/Python/split-bst.py b/Python/split-bst.py new file mode 100644 index 000000000..ced0963bc --- /dev/null +++ b/Python/split-bst.py @@ -0,0 +1,27 @@ +# Time: O(n) +# Space: O(h) + +# 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 splitBST(self, root, V): + """ + :type root: TreeNode + :type V: int + :rtype: List[TreeNode] + """ + if not root: + return None, None + elif root.val <= V: + result = self.splitBST(root.right, V) + root.right = result[0] + return root, result[1] + else: + result = self.splitBST(root.left, V) + root.left = result[1] + return result[0], root