Question

Given a binary tree

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

         1
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL

Solution 1 -- BFS

Key to the solution is to traverse tree level by level. Therefore, we can use BFS. Time complexity O(n), n is the number of nodes, and space cost is O(n).

 /**
* 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) {
// We can use BFS to solve this problem
List<TreeLinkNode> current = new ArrayList<TreeLinkNode>();
List<TreeLinkNode> next;
if (root == null)
return;
current.add(root);
while (current.size() > 0) {
next = new ArrayList<TreeLinkNode>();
int length = current.size();
for (int i = 0; i < length; i++) {
TreeLinkNode currentTreeNode = current.get(i);
if (i < length - 1)
currentTreeNode.next = current.get(i + 1);
else
currentTreeNode.next = null;
if (currentTreeNode.left != null)
next.add(currentTreeNode.left);
if (currentTreeNode.right != null)
next.add(currentTreeNode.right);
}
current = next;
}
}
}

Solution 2 -- Recursive

Consider for one node, the connection is done when:

1. Its left node (if exists) connect to its right node.

2. Its right node (if exists) connect to its next node's left child.

3. Its left child and right child are all connected.

Note the prerequisite for this problem is perfect binary tree (every parent has two children). Time complexity O(n), space cost O(1).

 /**
* 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;
if (root.left != null)
root.left.next = root.right;
if (root.right != null && root.next != null)
root.right.next = root.next.left;
connect(root.left);
connect(root.right);
}
}

Solution 3 -- Four Pointers

We can use four pointers to traverse the tree.

(referrence: ProgramCreek)

 /**
* 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;
TreeLinkNode lastHead = root;//prevous level's head
TreeLinkNode lastCurrent = null;//previous level's pointer
TreeLinkNode currentHead = null;//current level's head
TreeLinkNode current = null;//current level's pointer while (lastHead != null) {
lastCurrent = lastHead;
currentHead = lastHead.left;
current = lastCurrent.left;
while (lastCurrent != null && current != null) {
current.next = lastCurrent.right;
current = current.next;
lastCurrent = lastCurrent.next;
if (lastCurrent != null) {
current.next = lastCurrent.left;
current = current.next;
}
}
lastHead = currentHead;
currentHead = null;
}
}
}

Populating Next Right Pointers in Each Node 解答的更多相关文章

  1. Populating Next Right Pointers in Each Node II 解答

    Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  5. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  6. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  7. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  8. 【leetcode】Populating Next Right Pointers in Each Node II

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

  9. 【leetcode】Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

随机推荐

  1. C#优秀开源资料收集

    1. 把对命令行程序的调用封装起来,通过程序里进行输入,调用命令行程序的输出显示在程序中 http://www.codeproject.com/Articles/335909/Embedding-a- ...

  2. UVa133.The Dole Queue

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. OCP-1Z0-051-题目解析-第6题

    6. Examine the structure of the SHIPMENTS table: name                    Null        Type PO_ID      ...

  4. 19. Crontab

    一.Crontab 的使用 1.crontab 命令参数: -e   编辑该用户的计时器设置 -l 列出该用户的计时器设置 -r 删除该用户的计时器设置-u<用户名称> 指定要设定计时器的 ...

  5. android——ImageLoader添加缓存

    //给图片加入到缓存中.            DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc( ...

  6. html5 视频

    HTML规定了一种通过video元素来包含视频的标准方法 一段HTML5视频不可缺少的元素有video.controls等.. 直到现在,仍然不存在一项在网页上显示视频的标准. 大多数视频是通过fla ...

  7. html进阶css(1)

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  8. jQuery 双击事件(dblclick)时,不触发单击事件(click)

    我这是转载的文字 原文地址:http://www.cnblogs.com/wyblog/archive/2011/12/15/2289219.html 万恶的双击事件啊!! 在jQuery的事件绑定中 ...

  9. application windows are expected to have a root view controller错误

    产生这个提示的操作:在xcode4.6中创建一个名字为appTest空工程,create一个ios-application-empty application,直接编译运行 错误提示:虽然编译通过,也 ...

  10. How to Use Javascript to Control Quicktime

    参看下面链接: How to Use Javascript to Control Quicktime;