Easy Java Solution


  • 0
    S
        public int getMinimumDifference(TreeNode root) {
            if(root == null) return 0;
            ArrayList<Integer> arr = getVal(root, new ArrayList<Integer>());
            int mn = Integer.MAX_VALUE;
            Collections.sort(arr);
            for(int i = 0; i < arr.size()-1;i++){
                int a = arr.get(i+1)- arr.get(i);
                mn = Math.min(mn,a);
            }
            return mn;
        }
        public ArrayList<Integer> getVal(TreeNode root, ArrayList<Integer> arr ){
            if(root == null) return arr;
            arr.add(root.val);
            ArrayList<Integer> left = getVal(root.left,new ArrayList<Integer>());
            ArrayList<Integer> right = getVal(root.right,new ArrayList<Integer>());
            arr.addAll(left);
            arr.addAll(right);
            return arr;
            
        }
    }```

Log in to reply
 

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