原题
https://leetcode.com/problems/split-bst/
解法
递归, base case是根节点为空时, 返回[None, None]. 然后使用分治法, 分情况讨论.
代码
# 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]
if root.val == V:
temp = root.right
root.right = None
return [root, temp]
elif root.val > V:
small, large = self.splitBST(root.left, V)
root.left = large
return [small, root]
else:
small, large = self.splitBST(root.right, V)
root.right = small
return [root, large]
博客围绕LeetCode上的Split BST题目展开,介绍了解法。采用递归方法,以根节点为空作为base case返回[None, None],并运用分治法分情况讨论,还提及了代码部分,但未给出具体代码。
178

被折叠的 条评论
为什么被折叠?



