[JAVA] Recursive search / T : O(N), S : O(logN)


  • 0
    J

    class Solution {
    int first = Integer.MAX_VALUE;
    int second = Integer.MAX_VALUE;
    public int findSecondMinimumValue(TreeNode root) {
    dfs(root);

        return (second == Integer.MAX_VALUE) ? -1 : second;
    }
    
    
    public void dfs(TreeNode root){
        if(root == null)
            return;
        
        if(first > root.val){
            second = first;
            first = root.val;
        }
        else if(second > root.val && first < root.val){
            second = root.val;
        }
        
        dfs(root.left);
        dfs(root.right);
    }
    

    }


Log in to reply
 

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