[leetcode]Inorder Successor in BST
简单的利用递归来做
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def inorderTraverse(self, node: 'TreeNode', p: 'TreeNode', prev: ['TreeNode'], result: ['TreeNode']) -> 'TreeNode': if not node: return if result[0]: return if node.left: self.inorderTraverse(node.left, p, prev, result) if prev[0] and prev[0].val == p.val: result[0] = node prev[0] = node if node.right: self.inorderTraverse(node.right, p, prev, result) def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode': prev = [None] result = [None] self.inorderTraverse(root, p, prev, result) return result[0]