/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root==null) return null;
if(root.val<=p.val){
return inorderSuccessor(root.right,p);
}else{
TreeNode left=inorderSuccessor(root.left,p);
return left==null? root:left;
}
}
}
285. Inorder Successor in BST
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 原題 Given a binary search tree and a node in it, find the ...
- Given a binary search tree and a node in it, find the in-...
- inorder successor 就是中序遍歷的下一個節點,既是左孩子得父親, ret變量記錄root的父親結...
- Problem Given a binary search tree and a node in it, find...