Help needed : What is wrong with my solution?


  • 0
    N

    Can anyone please tell me what is wrong with my solution, I can't find out why it's not correct.

    public void connect(TreeLinkNode root) 
        {
            if(root == null)
            return;
            
            PriorityQueue<TreeLinkNode> nodes = new PriorityQueue<TreeLinkNode>();
            int count = 1;
            nodes.add(root);
            
            while(nodes.size()>0)
            {
                if(root.left != null)nodes.add(root.left);
                if(root.right != null)nodes.add(root.right);
                count--;
                if(count==0)
                {
                    nodes.poll().next = null;
                    count = nodes.size();
                }
                else
                {
                    nodes.poll().next = nodes.peek();
                }
            }
        }
    

Log in to reply
 

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