【Populating Next Right Pointers in Each Node II】cpp
题目:
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 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
代码:
/**
* 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) return;
deque<TreeLinkNode *> curr, next;
curr.push_back(root);
while ( !curr.empty() )
{
TreeLinkNode dummy(-);
TreeLinkNode *pre = &dummy;
while ( !curr.empty() )
{
TreeLinkNode *tmp = curr.front(); curr.pop_front();
pre->next = tmp;
if (tmp->left) next.push_back(tmp->left);
if (tmp->right) next.push_back(tmp->right);
pre = tmp;
}
pre->next = NULL;
std::swap(curr, next);
}
}
};
tips:
广搜思路(有些违规,因为不是const extra space)
每一层设立一个虚拟头结点,出队的同时pre->next = tmp
=====================================
学习了另外一种思路,可以不用队列的数据结构,这样就符合const extra space的条件了。代码如下:
/**
* 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) {
TreeLinkNode *pre;
TreeLinkNode *curr;
TreeLinkNode *next_first; // store next level's first not null node
curr = root;
while ( curr ){
// move to next tree level
TreeLinkNode dummy(-);
pre = &dummy;
next_first = NULL;
// connect the curr level
// record the first not null left or right child of the curr level as for the first node of next level
while ( curr ){
if ( !next_first ){
next_first = curr->left ? curr->left : curr->right;
}
if ( curr->left ){
pre->next = curr->left;
pre = pre->next;
}
if ( curr->right ){
pre->next = curr->right;
pre = pre->next;
}
curr = curr->next;
}
curr = next_first;
}
}
};
tips:
这套代码的大体思路是数学归纳法
1. root节点的root->next按照题意是NULL
2. 处理root的left节点和right节点之间的next关系
...如果知道了第n-1层的node之间的next关系,则可以得到第n层node节点之间的next关系...
按照这套思路,就可以写出上述的代码。
这里有两个细节需要注意:
a. next_first不一定是left还是right,这个要注意,只要next_first一直为NULL就要一直找下去。
b. 设立一个dummy虚node节点,令pre指向dummy,可以不用判断pre为NULL的情况,简化判断条件。
另,回想为什么level order traversal的时候必须用到队列,可能就是因为没有每一层之间的next关系。
==========================================
第二次过这道题,直接看常数空间复杂度的思路,重新写了一遍AC。
/**
* 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) {
TreeLinkNode* curr = root;
while ( curr )
{
TreeLinkNode* pre = new TreeLinkNode();
TreeLinkNode* next_level_head = NULL;
while ( curr )
{
if ( !next_level_head )
{
next_level_head = curr->left ? curr->left : curr->right;
}
if ( curr->left )
{
pre->next = curr->left;
pre = pre->next;
}
if ( curr->right )
{
pre->next = curr->right;
pre = pre->next;
}
curr = curr->next;
}
curr = next_level_head;
}
}
};
【Populating Next Right Pointers in Each Node II】cpp的更多相关文章
- 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】
本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【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】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【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 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- 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 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
随机推荐
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- string,stringbuilder,stringbuffer
String可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了存储数值不可改变的字符串. StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义 ...
- Python在金融,数据分析,和人工智能中的应用
Python在金融,数据分析,和人工智能中的应用 Python最近取得这样的成功,而且未来似乎还会继续下去,这有许多原因.其中包括它的语法.Python开发人员可用的科学生态系统和数据分析库.易于 ...
- 利用crontab自动更新SVN代码
shell.sh#!/bin/sh source ~/.bash_profile LANG=en_US.UTF- cd /opt/web/hzcms/ svn up >> /opt/web ...
- asp 301跳转代码
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location&quo ...
- tomcat 配置文件下载目录
tomcat可提供文件的直接下载.有两种方式. 第1种 放到ROOT 目录下 然后在网址中访问: http://ip:8080/download.zip 便可下载 第2种 希望使用自己的文件路径. 在 ...
- PHP导出excel文件
现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...
- 【自己动手】sublime text插件开发
今天是五四青年节,在此先祝大家节日快乐!!! --------------------------------------------华丽的分界线--------------------------- ...
- C# 平时碰见的问题【4】
1. 模糊查询 like的参数化写法 string keyword="value"; // 要模糊匹配的值 错误示范: sql: string strSql=" ...
- c++ 类与函数中static变量初始化问题(转)
首先static变量只有一次初始化,不管在类中还是在函数中..有这样一个函数: void Foo() { ; // initialize std::cout << a; a++; } 里的 ...