【一天一道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 ...
随机推荐
- MySql查询不区分大小写解决方案(两种)
当我们输入不管大小写都能查询到数据,例如:输入 aaa 或者aaA ,AAA都能查询同样的结果,说明查询条件对大小写不敏感. 解决方案一: 于是怀疑Mysql的问题.做个实验:直接使用客户端用sql查 ...
- Windows下createfile函数用GENERIC_READ访问模式打不开磁盘
这两天做毕设,快气死了!想读写磁盘扇区,我就百度了,都是这样写的: HANDLE hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive1" ...
- js error
0x800a0259 - JavaScript 运行时错误: 未知的运行时错误 <p id="navigatorInfo"></p> var txt = & ...
- 阿里云部署mongdb(CentOS)
配置包管理系统 (yum). Xshell登录Linux查看操作系统版本信息 lsb release -a 可以在官网选择对应的版本 :官网的安装指导文档http://docs.mongodb.org ...
- 152. Maximum Product Subarray(中等, 神奇的 swap)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 上篇:python的基本数据类型以及对应的常用方法(数字、字符串、布尔值)
为了日后便于查询,本文所涉及到的必记的基本字符串方法如下: "分隔符".join(字符串) #将字符串的每一个元素按照指定分隔符进行拼接.split("字符串&qu ...
- VMWare 虚拟机 共享文件夹
1.不能拷贝和直接拖拽文件至虚拟机系统中 解决办法: 通过共享文件夹的方式进行文件共享. (Win7 32位 10.0版本的虚拟机). ①:选择虚拟机 虚拟机 → 设置 如下图: ②: ...
- JS实现2048代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- PHP 安全 E-mail
PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> <body> <?php if (isset($_REQUEST['email'])) / ...
- PHP date() 函数
实例 格式化本地日期和时间,并返回格式化的日期字符串: <?php // Prints the dayecho date("l") . "<br>&qu ...