Java Iterative simple Solution


  • 0
    S
    public class Solution {
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            if(root == null || p == null) {
                return null;
            }
            TreeNode cur = root;
            TreeNode succ = null;
            while(cur != null) {
                if(cur.val <= p.val) {
                    cur = cur.right;
                }
                else {
                   succ = cur;
                   cur = cur.left;
                }
                
            }
            
            return succ;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.