Simple understanding inorder traverse solution


  • 0
    J
    class Solution {
        TreeNode pre = null;
        public boolean isValidBST(TreeNode root) {
            if (root == null) {
                return true;
            }
            boolean left = isValidBST(root.left);
            if (pre != null && root.val <= pre.val) {
                return false;
            }
            pre = root;
            boolean right = isValidBST(root.right);
            if (left && right) {
                return true;
            }
            return false;
        }
    }
    

    Traverse inorder and record the previous node at the same time.


Log in to reply
 

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