给定一个二叉树
    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }
填充他的每个 next(下一个)指针,让这个指针指向其下一个右侧节点。如果找不到下一个右节点,则应该将 next(下一个)指针设置为 NULL。
初始状态下,所有 next(下一个)指针 都被设置为 NULL。
注意事项:
    您只能使用恒定的额外空间。
    你可以假设它是一棵完美二叉树(即所有叶子都在同一水平上,每个父节点有两个孩子)。
例如,鉴于以下完美二叉树,
         1
       /  \
      2    3
     / \  / \
    4  5  6  7
调用你的函数后,该树应该变成这样:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \  / \
    4->5->6->7 -> NULL

详见:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/

Java实现:

递归实现:

/**
* 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;
}
if(root.left!=null){
root.left.next=root.right;
}
if(root.right!=null){
root.right.next=root.next!=null?root.next.left:null;
}
if(root.left!=null){
connect(root.left);
}
if(root.right!=null){
connect(root.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;
}
LinkedList<TreeLinkNode> que=new LinkedList<TreeLinkNode>();
que.offer(root);
while(!que.isEmpty()){
//记录本层节点的个数
int size=que.size();
for(int i=0;i<size;++i){
TreeLinkNode cur=que.poll();
//最后一个节点的next是null,不做处理
if(i<size-1){
TreeLinkNode next=que.peek();
cur.next=next;
}
if(cur.left!=null){
que.offer(cur.left);
}
if(cur.right!=null){
que.offer(cur.right);
}
}
}
}
}

116 Populating Next Right Pointers in Each Node 每个节点的右向指针的更多相关文章

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

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

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

  3. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

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

  5. LeetCode OJ 116. Populating Next Right Pointers in Each Node

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

  6. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  7. 116. Populating Next Right Pointers in Each Node

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

  8. leetcode 116 Populating Next Right Pointers in Each Node ----- java

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

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

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

随机推荐

  1. C++中map容器的说明和使用技巧

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明 1 头文件 #include <map> 2 定义 ma ...

  2. 【应用】图片翻转js

    图片翻转:图片随着鼠标指针划过进行替换 <img src="example.gif" onmouseover="this.src='exampleTwo.gif'& ...

  3. CKEDITOR 默认最大化

    createEditor("newsEditer"); //创建一个editer //editer 最大化 CKEDITOR.instances["newsEditer& ...

  4. 更换ubuntu apt-get源

    原文地址:http://www.cnblogs.com/zhangpengshou/p/3591387.html 为了优化ubuntu软件安装/更新速度,我测试了国内几家apt源的速度,发现北京交大的 ...

  5. HDU1852 Beijing 2008(快速幂+特殊公式)

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  6. AutoIT:界面与自动化操作结合来简化日常劳动: .Net Reactor验证License,设置License,创建License,截图AutoIt自动化实现。(七)

    版本六中存在一个显著问题是: 当exe文件生存之后,运行的时候,通过consoleWrite函数打印出来的数据是无法展示出来的.这就存在一个问题:当运行失败的时候,我还是看不到任何log信息. 于是, ...

  7. Python下使用Psyco模块优化运行速度

    今天介绍下Psyco模块,Psyco模块可以使你的Python程序运行的像C语言一样快.都说Python语言易用易学,但性能上跟一些编译语言(如C语言)比较要差不少,这里可以用C语言和Python语言 ...

  8. yolo-开源数据集coco kitti voc

    1.kitti数据集(参考博客:https://blog.csdn.net/jesse_mx/article/details/65634482  https://blog.csdn.net/baoli ...

  9. C++ 两款静态检查工具

    pclint(收费) http://www.gimpel.com/html/pcl.htmpc-lint是资格最老,最强力的代码检查工具,但是是收费软件,并且配置起来有一点点麻烦. ccpchecke ...

  10. doc命令大全

    不是原创的,但基本上收入了各个网站dos命令了基本上可以作为电子书使用,希望对各位有用net use \\ip\ipc$ " " /user:" " 建立IPC ...