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. 〖Linux〗OK6410a蜂鸣器的驱动程序编写全程实录

    最近在看一本书,受益匪浅,作者是李宁,下边是编写本次蜂鸣器的全程实录: 1. 了解开发板中的蜂鸣器 1) 查看蜂鸣器buzzer在底板中的管脚信息 2) 查看蜂鸣器在总线中的信息 3) 翻看S3C64 ...

  2. 导出CCS3.3数据及使用matlab处理的方法

    CCS3.3是一款DSP的集成开发环境(IDE).在做DSP开发时,为验证算法.经常须要使用matlab进行算法验证,验证算法就须要数据.因此,一种交互的方法是: 使用DSP开发板连接CCS 用CCS ...

  3. Toast连续弹出的问题

    public class CommUtils { private static Toast toast = null; public static void showToast(int text) { ...

  4. 6种.net分布式缓存解决方式

    6种.net分布式缓存解决方式 1.     使用内置ASP.NET Cache (System.Web.Caching) : https://msdn.microsoft.com/en-us/lib ...

  5. Opcode查看利器之vld

    简介 在PHP的生命周期中 词法分析(zend_language_scanner),将PHP代码转换为语言片段(Tokens) 语法分析(zend_language_parser)将Tokens转换成 ...

  6. HDUOJ---3743Frosh Week(BIT+离散化)

    Frosh Week Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDUOJ----The Number Off of FFF

    The Number Off of FFF Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. MFC异常处理的问题

    参考文献:http://technet.microsoft.com/zh-cn/library/t078xe4f(v=vs.85).aspx MFC中异常处理的语法和语义构建在标准C++异常处理语法和 ...

  9. ubuntu修改固定ip

    1.vi /etc/network/interfasces,添加红框内的内容:

  10. 10个你必须知道的jQueryMobile代码片段(转)

    1.在列表项和按钮上禁用文本截断      如果你的列表项或者按钮上是一个很长的文本,它将会被jQuery Mobile自动截断,要禁用这个截断设置,需要在CSS选择器上添加属性"white ...