Populating Next Right Pointers in Each Node Total
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

我们可以用递归处理左右子树. 这个算法可能会消耗O(h)的栈空间。

 /* 试一下 recursion */
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} rec(root);
} public void rec(TreeLinkNode root) {
if (root == null) {
return;
} if (root.left != null) {
root.left.next = root.right;
} if (root.right != null) {
root.right.next = root.next.left;
} rec(root.left);
rec(root.right);
}

2014.1229 redo:

 /*
3. A recursion version.
*/
public void connect3(TreeLinkNode root) {
if (root == null || root.left == null) {
return;
} root.left.next = root.right;
root.right.next = root.next == null ? null: root.next.left; connect(root.left);
connect(root.right);
}

SOLUTION 2

用层次遍历也可以相当容易解出来,而且这种方法用在下一题一点不用变。

但是 这个解法不能符合题意。题目要求我们使用 constant extra space.

 /*
* 使用level traversal来做。
* */
public void connect1(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode dummy = new TreeLinkNode(0);
Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root);
q.offer(dummy); while (!q.isEmpty()) {
TreeLinkNode cur = q.poll();
if (cur == dummy) {
if (!q.isEmpty()) {
q.offer(dummy);
}
continue;
} if (q.peek() == dummy) {
cur.next = null;
} else {
cur.next = q.peek();
} if (cur.left != null) {
q.offer(cur.left);
} if (cur.right != null) {
q.offer(cur.right);
}
}
}

 2014.1229 redo:

1.

 /*
1. Iterator.
*/
public void connect1(TreeLinkNode root) {
if (root == null) {
return;
} Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size(); for (int i = 0; i < size; i++) {
TreeLinkNode cur = q.poll(); // ERROR 2: forget to determine if root don't have left and right.
if (cur.left == null) {
return;
} cur.left.next = cur.right;
cur.right.next = cur.next == null ? null : cur.next.left;
// bug 1: should put the offer inside the for loop
q.offer(cur.left);
q.offer(cur.right);
}
}
}

2.

 /*
2. Iterator. More simple version.
*/
public void connect2(TreeLinkNode root) {
if (root == null) {
return;
} Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size(); for (int i = 0; i < size; i++) {
TreeLinkNode cur = q.poll(); // bug 1: should judge the size!
cur.next = (i == size - 1) ? null: q.peek(); if (cur.left != null) {
q.offer(cur.left);
q.offer(cur.right);
}
}
}
}

SOLUTION 3

把层次遍历修改一下,就是下面的解法了,我们使用2个循环,一个指针P1专门记录每一层的最左边节点,另一个指针P2扫描本层,把下一层的链接上。

下层链接完成后,将P1移动到它的左孩子即可。

这个算法的空间复杂度是O(1). 没有额外的空间。

 /*
The version that only has O(1) space complexity.
*/
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} Iterator(root);
} public void Iterator(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode leftEnd = root; while(leftEnd != null) {
// go through the current level and link the next level.
TreeLinkNode cur = leftEnd;
while (cur != null) {
if (cur.left == null) {
break;
} cur.left.next = cur.right;
// 一定要记得判null.
cur.right.next = cur.next == null ? null: cur.next.left; cur = cur.next;
} // get to the next level.
leftEnd = leftEnd.left;
}
}

 2014.1229 redo:

 /*
4. Another constant iterator version.
*/
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode leftEnd = root;
while (leftEnd != null && leftEnd.left != null) {
TreeLinkNode cur = leftEnd;
while (cur != null) {
cur.left.next = cur.right;
cur.right.next = cur.next == null ? null: cur.next.left; cur = cur.next;
} leftEnd = leftEnd.left;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/Connect_2014_1229.java

LeetCode: Populating Next Right Pointers in Each Node 解题报告的更多相关文章

  1. 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

  3. [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 ...

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

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

  5. 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 ...

  6. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  7. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

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

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

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

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

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

随机推荐

  1. 在命令行上 使用 mutt, fetchmail, maildrop, msmtp 收发邮件

    基于shell 现在已经有了 Mail.app, Thunderbird, Outlook 这些图形化工具能很方便的处理邮件,为啥还需要 mutt 这种命令行文本方式的邮件工具呢?mutt 的一个优势 ...

  2. UVa 1303 - Wall

    题目:有非常多点.修一座最短的围墙把素有点围起来,使得全部点到墙的距离不小于l. 分析:计算几何,凸包. 假设.没有距离l的限制.则答案就是凸包的周长了.有了距离限制事实上是添加了2*π*l. 证明: ...

  3. exception http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

      http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed w ...

  4. 创建简单的Telnet实例

    step1.先加入库SuperSocket.Common.dll, SuperSocket.SocketBase.dll, SuperSocket.SocketEngine.dll,log4net.d ...

  5. 同域名不同端口应用共享sessionid问题解决办法

    相同域名共享sessionid 如 www.abc.com www.abc.com/test www.abc.com:8080 www.abc.com:8989 这些地址由于是在相同的域名下,所以共享 ...

  6. 使用nginx生成缩略图

    nginx中可以使用 --with-http_image_filter_module 这个模块,今天发现在github上发现国人开发的一款模块 模块同时支持 Nginx 和 tengine 本ngin ...

  7. 通过iscsi协议使用ceph rbd

    转自:http://blog.csdn.net/wytdahu/article/details/46545235 ceph很早就已经支持通过iscsi协议来使用rbd,这篇博文对此做下演示,并且使用O ...

  8. img图片自适应宽和高[转]

    控制缩略图常见的是JS来控制,还有就是最直接的方法定义img的宽高:下面两种方法自适应宽和高,zhenzhai推荐使用CSS方法:一.CSS方法:主 要是在CSS设置最小值和最大值(max-width ...

  9. DBA_实践指南系列9_Oracle Erp R12应用补丁AutoPatch/AutoControl/AutoConfig(案例)

    2013-12-09 Created By BaoXinjian

  10. tp表单的提交与验证

    一.控制器如下 引用use app\index\model\User; //注意模型类名不能和控制器类名相同 public function index(){ return $this->fet ...