Description:

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 看到这个题目首先想到的是按层遍历二叉树,然后对每一层做处理,但是仔细读题发现这个做法不好,因为题目要求空间复杂度是O(1):
  • 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).

这样可以很容易想到一个非常简单类似前序遍历的方法:

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

虽然上边这种解法是可以AC的,但是也不符合题意,因为这种解法要消耗O(logh)的辅助递归栈空间。

这样的话就要消去递归用循环来写就行了。这样空间复杂度就是O(1),时间复杂度是O(logh);

代码:

/**
* 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 || root.left == null) {
return ;
} TreeLinkNode cur; while(root.left != null) { //深度优先遍历左子树,目的是获取每一层的第一个节点
cur = root;
while(cur != null) { //遍历一层的每一个节点,并用next指针连接
cur.left.next = cur.right;
if(cur.next != null) {
cur.right.next = cur.next.left;
}
cur = cur.next;
}
root = root.left;
} return;
}
}

这题数据很水,递归也能过。

LeetCode——Populating Next Right Pointers in Each Node的更多相关文章

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

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

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

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

  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 II @ Python

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

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

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

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

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

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

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

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

    Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode {      Tree ...

  10. [LeetCode] [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 ...

随机推荐

  1. Lua 中pairs与ipairs区别

    local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...

  2. linux 获取随机数的办法

    1.1.1 inux随机数的办法  http://www.2cto.com/kf/201410/342717.html 方法一.[root@ob ~]# date +%N  %N纳秒  随机获取的九位 ...

  3. Hibernate- QBC-基本查询

    01.环境搭建 02.基本查询 1.方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt &g ...

  4. there are 0 datanode.....

    当时执行hive的导入数据load data  inpath "XXXX" into table.....的时候发现总是导不进去,最后试了下简单的从Linux 到 HDFS上传文件 ...

  5. numpy得到数组的index

    itemindex = numpy.where(array==item)

  6. Hibernate一级缓存、二级缓存以及查询缓存的关系

    转载自http://blog.csdn.net/maoyeqiu/article/details/50209893 前两天总结了一下二级缓存和查询缓存的关系,但是又有一个新的问题,就是查询缓存缓存到二 ...

  7. JVM内存模型 小小结

    可以看一下我的另一篇总结 JVM运行时数据区与JVM堆内存模型小结 推荐一篇文章,尚学堂的 Java内存模型深度解读 . 不方便全文转载,就摘录下吧. 以往的认知都是以基本类型.引用类型.常量.方法等 ...

  8. linux ffmpeg编译配置安装详解

    http://www.111cn.net/sys/linux/53039.htm ffmpeg是一开源的可跨平台使用的一个图形处理插件,这可以进行录制.转换以及流化音视频,同时可以对视频进行截图,下面 ...

  9. javascript -- 原型对象

    原型对象: 每个对象都有一个参考对象,这个参考对象称之为原型对象.原型对象有自己的属性和方法.当A是B的原型对象时,那 么B拥有A中的所有属性和方法. 原型对象的工作原理: 使用原型对象定义一个新的对 ...

  10. Qt 定时器Timer使用

    From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/ 1.新建Gui工程,在主界面上添加一个标签labe ...