Java BFS solution


  • 0
    X
        public int findSecondMinimumValue(TreeNode root) {
            if (root==null){
                return -1;
            }
            PriorityQueue<TreeNode> pq = new PriorityQueue<>(Comparator.comparingInt(n -> n.val));
            pq.add(root);
            int min = root.val;
            while (!pq.isEmpty()){
                TreeNode n = pq.poll();
                if (n.val!=min){
                    return n.val;
                }
                if (n.left!=null) {
                    pq.add(n.left);
                }
                if (n.right!=null){
                    pq.add(n.right);
                }
            }
            return -1;
        }
    

Log in to reply
 

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