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

改变树的结构。

第一种用队列,并不是很快。

/**
* 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 ;
Queue queue = new LinkedList<TreeLinkNode>(); queue.add(root); while( !queue.isEmpty() ){ TreeLinkNode node = (TreeLinkNode) queue.poll(); if( node.left != null){
node.left.next = node.right;
if( node.next != null )
node.right.next = node.next.left;
queue.add(node.left);
queue.add(node.right);
}
} }
}

不使用队列就会达到最快。

/**
* 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 node1 = root,node2 = root;
while( node2.left != null ){ node1 = node2.left;
while( node2.next != null ){
node2.left.next = node2.right;
node2.right.next = node2.next.left;
node2 = node2.next;
}
node2.left.next = node2.right;
node2 = node1; } }
}

leetcode 116 Populating Next Right Pointers in Each Node ----- java的更多相关文章

  1. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  3. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

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

  4. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

  5. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

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

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

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

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

  8. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. CoHTMLDocument

    http://blog.csdn.net/dlwxn/article/details/2860329 http://www.itnose.net/detail/120267.html 不知道是 线程内 ...

  2. jquery animate()方法使用的注意事项

    当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margi ...

  3. NOP初学记录

    1.  介绍的话不多说了.直接先来简单的安装跟配置先以3.6版本为例: 附带官网地址: http://www.nopcommerce.com   自行下载. 中文网:http://www.nopchi ...

  4. 银行支票和汇票中使用的专用字体MICR E13B条形码控件字体

    MICR E13B条形码控件字体是一种在美国.加拿大.波多黎各.巴拿马.英国和其它少数国家的银行支票和汇票中使用的专用字体,主要用来打印适用于磁性和光学字符识别系统的MICR字符.MICR E13B条 ...

  5. Python学习路程day4

    迭代器&生成器 迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...

  6. resolve some fragment exception

    1.android fragment not attached to activity http://blog.csdn.net/walker02/article/details/7995407 if ...

  7. hdu 2027

    ps:发现语文理解能力不行也是醉醉的....是每个测试实例空行....空!行!不是空格! 还有就是gets才能吸收空格,而scanf不能. 代码: #include "stdio.h&quo ...

  8. (spring-第10回【IoC基础篇】)InstantiationStrategy--实例化Bean的第三大利器

    Bean的实例化整个过程如下图: : 其中,BeanDefinition加入到注册表中,并由BeanFactoryPostProcessor的实现类处理后,需要由InstantiationStrate ...

  9. python 安装操作 MySQL 数据库.

    以ubuntu和mysql为例 检查自己的机器上面有没有安装数据库 xpower@xpower-CW65S:~$ sudo service mysql start [sudo] xpower 的密码: ...

  10. River Crossing 简单的动态规划 .

    第一行 t  表示有几组测试数据 . 每组测试数据的 第一行是 n, m   .     然后 下面有n行数据  . 题意:有1个人和N只羊要过河.一个人单独过河花费的时间是M,每次带一只羊过河花费时 ...