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. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. java.lang.ExceptionInInitializerError

    java.lang.ExceptionInInitializerError at com.csdhsm.compiler.test.DevTest.testReadInput(DevTest.java ...

  3. Session初识

    web服务器没有短期记忆,所以需要使用session来跟踪用户的整个会话活动.会话管理有3种解决方案: 1)使用隐藏域(很少使用) 在显示页面中使用隐藏域来保存会话ID.例如,在JSP中将input标 ...

  4. [转]Vimium快捷键

    from: http://www.cppblog.com/deercoder/archive/2011/10/22/158886.html 今天下午折腾了一下Chrome下面的一个插件Vimium的使 ...

  5. Visual Studio环境色调配色

    据说对眼睛有好处的色调配色 vs 色调rgb 199,240,214 色调为84,饱和度为91,亮度为205 色调为85,饱和度为123,亮度205: “色调”由160更改为75-85之间——> ...

  6. GoldenGate中使用strcat和strext进行数据转换

    在OGG中可以对源字段的内容进行合并或拆分,从而实现类似于“ETL”的功能.strcat(s1,s2,s3,,,):用于合并字串:strext(str, start, end):用于获取指定位置的字串 ...

  7. pyqt5 笔记(四)cx_Freeze 实现代码打包exe

    下载地址:https://pypi.python.org/pypi/cx_Freeze 教程:http://www.cnblogs.com/xinzaitian/archive/2010/12/10/ ...

  8. 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》

    //同一个类中不同方法之间的调用相关问题(省略的类名或者this) class A { public void B() { System.out.println("b方法运行"); ...

  9. BZOJ 1912 巡逻

    重赋边权. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  10. Mac OS环境变量配置(Android Studio之Gradle)

    以gradle环境变量配置为例: Android Studio 自带的gradle路径为: /Applications/Android\ Studio.app/Contents/gradle/grad ...