Why this is fals? (Java solution)

  • 0

    This is my code:

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

    and I get this wrong answer.. and I don't get why.. could anyone hlep me out?


    Output: true

    Expected: false

  • 1

    All the right nodes value should be less than the root,
    For 6, it should be larger than 10.

Log in to reply

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