这是“每个节点的右向指针”问题的进阶。
如果给定的树可以是任何二叉树,该怎么办?你以前的解决方案仍然有效吗?
注意:
    你只能使用恒定的空间。
例如,
给定以下二叉树,
         1
       /  \
      2    3
     / \    \
    4   5    7
调用你的函数后,树应该看起来像这样:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

详见:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/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;
}
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);
}
}
}
}
}

参考:https://segmentfault.com/a/1190000003465911

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

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

  2. leetCode 116.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】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  5. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  6. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

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

  7. 117. Populating Next Right Pointers in Each Node II

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

  8. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  9. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. CSU - 1803 —— 数学题

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Description  给出正整数 n 和 m,统计满足以下条件的正整数对 ...

  2. Springboot框架中request.getInputStream()获取不到上传的文件流

    Springboot框架中用下面的代码,使用request.getInputStream()获取不到上传的文件流 @PostMapping("/upload_img") publi ...

  3. read,write,accept,connect 超时封装

    //read操作加上超时时间. 1 int read_timeout(int fd, void *buf, uint32_t count, int time) { ) { fd_set rSet; F ...

  4. getElementsByName()获取标签时的注意

    var aDiv = document.getElementsByTagName('div');//获取的标签名注意你下面用的是哪一个div的标签名,例如 aDiv[0] 才可以: <!-- 注 ...

  5. 【Linux学习】Linux文件系统3—文件操作命令

    Linux文件系统3-文件操作命令 Linux文件操作命令主要有: cd:    改变目录位置 pwd:  显示当前目录的绝对路径 ls:    显示文件名称.属性等 -a 列出全部文件 -l  列出 ...

  6. Flutter实战视频-移动电商-50.持久化_shared_preferences

    50.持久化_shared_preferences 当app关掉了.再进去的时候 ,购物车的内容还是存在. sqflite提供这个来操作SQLite数据库 flutter提供三种持久化的工具 今天要学 ...

  7. Metatable In Lua 浅尝辄止

    http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html 什么是Metatable Lua中Metatable这个概念, 国内将他翻译为 ...

  8. Qt解析CSV文件

    最近需要解析Excel文件,于是顺带写了解析CSV的代码 定义数据类型LX::Sheet #ifndef LX_H #define LX_H #include <QString> #inc ...

  9. Codevs 3409 搬运礼物

    3409 搬运礼物 CodeVS原创  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description 小浣熊松松特别喜欢交 ...

  10. [Xcode 实际操作]九、实用进阶-(17)使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色

    目录:[Swift]Xcode实际操作 本文将演示如何使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色. 在项目文件夹[DemoApp]上点击鼠标右键 ->[New Fi ...