Java Modular solution


  • 0
    class Solution {
        public int findSecondMinimumValue(TreeNode root) {
            if(root == null) return -1;
            Set<Integer> values = new HashSet<>();
            readValues(values, root);
            if(values.size() < 2) return -1;
            return findSecondSmallest(values);
        }
        
        private void readValues(Set<Integer> values, TreeNode root){
            if(root == null) return;
            if(root.left == null & root.right == null) values.add(root.val);        
            readValues(values, root.left);
            readValues(values, root.right);
        }
        
        private int findSecondSmallest(Set<Integer> values){
            List<Integer> list = new ArrayList<>(values);
            Collections.sort(list);
            return list.get(1);
        }
    }
    

Log in to reply
 

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