这道题一定要理解dfs是递归到了叶子节点然后回溯,否则就做不出来

Py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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):

if not root: return [None, None]

if root.val > V:
left, right = self.splitBST(root.left, V)
root.left = right
return [left, root]
else :
left, right = self.splitBST(root.right, V)
root.right = left
return [root, right]