Short Java BFS Solution


  • 4
    T
    public void connect(TreeLinkNode root) {
        TreeLinkNode queue = root;
        while (queue != null) {
            TreeLinkNode level = new TreeLinkNode(0);
            TreeLinkNode current = level;
            while (queue != null) {
                if (queue.left != null) {
                    current.next = queue.left;
                    current = current.next;
                }
                if (queue.right != null) {
                    current.next = queue.right;
                    current = current.next;
                }
                queue = queue.next;
            }
            queue = level.next;
        }
    }

  • 0
    This post is deleted!

Log in to reply
 

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