Leetcode-Populating Next Right Pointer in Binary Tree II

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /        2    3
     / \        4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \        4-> 5 -> 7 -> NULL
Solution:
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root==null)
            return;

        root.next = null;
        TreeLinkNode levelHead = root;

        while (true){
            boolean hasChild = false;
            TreeLinkNode cur = levelHead;
            TreeLinkNode nextLevelPre = null;
            TreeLinkNode nextLevelHead = null;
            while (cur!=null){
                if (cur.left!=null){
                    hasChild = true;
                    if (nextLevelPre!=null){
                        nextLevelPre.next = cur.left;
                        nextLevelPre = cur.left;
                    } else {
                        nextLevelPre = cur.left;
                        nextLevelHead = cur.left;
                    }
                } 

                if (cur.right!=null){
                    hasChild = true;
                    if (nextLevelPre!=null){
                        nextLevelPre.next = cur.right;
                        nextLevelPre = cur.right;
                    } else {
                        nextLevelPre = cur.right;
                        nextLevelHead = cur.right;
                    }
                }

                cur = cur.next;
            }
            if (hasChild)
                nextLevelPre.next = null;

            //If reach the last level, then stop.
            if (!hasChild)
                break;

            //Move to next level.
            levelHead = nextLevelHead;
        }

        return;
    }
}

At each level, after we construct the link list, we have a linked list to visit all nodes in this level. Then we can visit all child nodes in the next level along this linked list.This is a very smart way.

Note: For this question, we need to be careful about where the first child node in the next level is.

时间: 2024-10-01 01:28:33

Leetcode-Populating Next Right Pointer in Binary Tree II的相关文章

[Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

LeetCode OJ - Minimum && Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

【一天一道LeetCode】#104. Maximum Depth of Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

leetcode 刷题之路 78 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 非递归方式实现二叉树的后序遍历. 思路:使用栈辅助实现,首先将根节点入栈,然后以栈是否为

leetcode 刷题之路 79 Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 非递归实现树的前序遍历.前序遍历比较简单,注意入栈的时候应该按照右孩子节点在前的顺序,这样

LeetCode: Populating Next Right Pointers in Each Node II [117]

[题目] Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bin