【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Follow up for problem “Populating Next Right Pointers in Each Node”.
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
 For example,
Given the following binary tree,1 / \ 2 3 / \ \ 4 5 7After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
(二)解题
参考:【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node 
没想到昨天做的解法直接把今天的题给解了。一模一样的代码,请跳转到上题的解题思路:
/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(root==NULL) return;
        queue<TreeLinkNode *> myque;
        myque.push(root);
        while(!myque.empty())
        {
            queue<TreeLinkNode *> temp_que;
            TreeLinkNode *pre = NULL;
            while(!myque.empty())
            {
                TreeLinkNode *temp = myque.front();
                myque.pop();
                if(pre==NULL) pre = temp;//相当于每一层的头节点
                else {
                    pre->next = temp;//用next指针连接每一层的节点
                    pre = temp;
                }
                if(temp->left!=NULL) temp_que.push(temp->left);//下一层
                if(temp->right!=NULL) temp_que.push(temp->right);//下一层
            }
            pre->next=NULL;//最后一个节点的next需要指向NULL
            myque=temp_que;//进入下一层操作
        }
    }
};
【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II的更多相关文章
- [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 ...
 - Java for LeetCode 117 Populating Next Right Pointers in Each Node II
		
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
 - 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 ...
 - Leetcode#117 Populating Next Right Pointers in Each Node II
		
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
 - 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
 - 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
		
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
 - 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 ...
 - 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
		
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
 - [Leetcode Week15]Populating Next Right Pointers in Each Node II
		
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
 - 【leetcode】Populating Next Right Pointers in Each Node II
		
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
 
随机推荐
- PowerBI 第九篇:修改查询
			
在PowerBI的查询编辑器中,用户可以使用M语言修改Query,或修改Query字段的类型,或向Query中添加数据列(Column),对Query进行修改会导致PowerBI相应地更新数据模型(D ...
 - django 发送手机验证码
			
一.流程分析: 1.用户在项目前端,输入手机号,然后点击[获取验证码],将手机号发到post到后台. 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本,让短信运营商 ...
 - 手写JAVA虚拟机(二)——实现java命令行
			
查看手写JAVA虚拟机系列可以进我的博客园主页查看. 我们知道,我们编译.java并运行.class文件时,需要一些java命令,如最简单的helloworld程序. 这里的程序最好不要加包名,因为加 ...
 - 一口一口吃掉Hibernate(七)——继承映射
			
前几篇博文中讲到了常用的几种关联映射.其实hibernate中还有一种"省劲儿"的映射,那就是--"继承映射". 学了这么多的关系映射了,继承映射,从字面上也能 ...
 - 使用svn无法cleanup和lock问题
			
step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe 找到 Precompiled Binaries for W ...
 - python中修改字符串的几种方法
			
在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...
 - [转]关于OpenGL的绘制上下文
			
[转]关于OpenGL的绘制上下文 本文转自(http://www.cnblogs.com/Liuwq/p/5444641.html) 什么是绘制上下文(Rendering Context) 初学Op ...
 - FJUT第四周寒假作业[JL]最后的晚餐(动态规划)
			
题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P4 [JL]最后的晚餐 TimeLimit:1000MS MemoryLimit:100 ...
 - JavaScript原型与原型链
			
一.数据类型 JavaScript的数据类型可以分为基本数据类型和引用数据类型. 基本数据类型(6种) String Number Boolean null undefined Symbol(ES6) ...
 - 通讯协议序列化解读(二) protostuff详解教程
			
上一篇文章 通讯协议序列化解读(一):http://www.cnblogs.com/tohxyblog/p/8974641.html 前言:上一面文章我们介绍了java序列化,以及谷歌protobu ...