2015年11月5日星期四

Leetcode 285 Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null.
Solution 1: binary search the value>v
 public class Solution {  
   public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {  
     TreeNode x=root;  
     TreeNode res=null;  
     while (x!=null) {  
       if (x.val>p.val) {  
         res=x;  
         x=x.left;  
       }  
       else x=x.right;  
     }  
     return res;  
   }  
 }  

没有评论:

发表评论