JAVA Easy to Understand Recursive Solution


  • 0
    class Solution {
        public int findSecondMinimumValue(TreeNode root) {
            if(root == null) return -1;  
            return findSecond(root, root.val); 
        }
        private int findSecond(TreeNode root, int x) {
            if (root == null) return -1;
            if (root.val != x) return root.val;
            else {
                int left = findSecond(root.left, x);
                int right = findSecond(root.right, x);
                if (left == -1) return right;
                else if (right == -1) return left;
                else return Math.min(left, right);
            }
        }
    }

Log in to reply
 

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