设计一个算法,找出二叉搜索树中指定节点的“下一个”节点(也即中序后继)。
如果指定节点没有对应的“下一个”节点,则返回null。
示例 1:
输入: root = [2,1,3], p = 1
2
/ \
1 3
输出: 2
示例 2:
输入: root = [5,3,6,2,4,null,null,1], p = 6
5<br /> / \<br /> 3 6<br /> / \<br /> 2 4<br /> / <br />1
输出: null
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/class Solution {public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {if (root == null) return null;// root.val <= p.val 后继在右子树if (root.val <= p.val) return inorderSuccessor(root.right, p);//root.val > p.val 后继要么在左子树要么就是root本身TreeNode next = inorderSuccessor(root.left, p);return next == null ? root : next;}}
