Simple Inorder traversal solution in JAVA


  • 0
    Z

    pretty straightforward.

    class Solution {
        private int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        public int findSecondMinimumValue(TreeNode root) {
            inorderTraversal(root);
            return min2 == Integer.MAX_VALUE ? -1 : min2;
        }
        private void inorderTraversal(TreeNode root) {
            if (root != null) {
                inorderTraversal(root.left);
                
                if (root.val < min1) {
                    min2 = min1;
                    min1 = root.val;
                } else if (root.val > min1 && root.val < min2) {
                    min2 = root.val;
                }
                inorderTraversal(root.right);
            }
        }
    }
    

Log in to reply
 

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