Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->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 dfs(TreeLinkNode* root1,TreeLinkNode* root2){
if(!root1) return; root1->next = root2; dfs(root1->left,root1->right); if(!root2) return; dfs(root1->right,root2->left);
dfs(root2->left,root2->right);
}
void connect(TreeLinkNode *root) {
if(!root) return ;
dfs(root->left,root->right);
}
};
Populating Next Right Pointers in Each Node II
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 ; TreeLinkNode* head = NULL;
TreeLinkNode* tail = NULL;
TreeLinkNode* cur = root; while(cur){
if(cur->left){
if(tail){
tail->next = cur->left;
tail = cur->left;
}else{
tail = cur->left;
head = cur->left;
}
}
if(cur->right){
if(tail){
tail->next = cur->right;
tail = cur->right;
}else{
tail = cur->right;
head = cur->right;
}
}
cur = cur->next;
if(!cur){
cur = head;
tail = NULL;
head = NULL;
}
}
}
};
Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II的更多相关文章
- 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 ...
- Node.app – 用于 iOS App 开发的 Node.js 解释器
Node.app 是用于 iOS 开发的 Node.js 解释器,它允许最大的代码重用和快速创新,占用资源很少,为您的移动应用程序提供 Node.js 兼容的 JavaScript API.你的客户甚 ...
- Node.js实战项目学习系列(5) node基础模块 path
前言 前面已经学习了很多跟Node相关的知识,譬如开发环境.CommonJs,那么从现在开始要正式学习node的基本模块了,开始node编程之旅了. path path 模块提供用于处理文件路径和目录 ...
- [Node.js] 00 - Where do we put Node.js
Ref: 前后端分离的思考与实践(五篇软文) 其实就是在吹淘宝自己的Midway-ModelProxy架构. 第一篇 起因 为了提升开发效率,前后端分离的需求越来越被重视, 同一份数据接口,我们可以定 ...
- Node.js 学习笔记(一)--------- Node.js的认识和Linux部署
Node.js 一.Node.js 简介 简单的说 Node.js 就是运行在服务端的可以解析并运行 JavaScript 脚本的软件. Node.js 是一个基于Chrome JavaScript ...
- Node“getTextContent() is undefined for the type Node”处理办法
最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...
- nohup /usr/local/node/bin/node /www/im/chat.js >> /usr/local/node/output.log 2>&1 &
nohup和&后台运行,进程查看及终止 &后台运行 登出ssh终端,进程会被自动kill掉 但是nohup >>XX.log 2>&1 & 登出终 ...
- node基础09:第2个node web服务器
1.同时输出文字与图片 在前几个小课程中,我会学会了 从服务器中读取文字字符,并且向浏览器中输出 从服务器中读取图片文件,并且向浏览器中输出 这节课中,我学会了同时向浏览器输出文字,图片.对此,我感到 ...
- [Node.js] Using npm link to use node modules that are "in progress"
It is some times convenient, even necessary, to make use of a module that you are working on before ...
随机推荐
- Windows7 无法打开ASA SSL VPN和ASDM首页
原文地址:Windows7 无法打开ASA SSL VPN 首页和无法打开 ASDM GUI 页面作者:futhy windows 7 无法打开ASA SSL VPN 和AS ...
- 自己对WSO2 ESB 见解
这周没想到要更新什么内容,就把我最近工作接触的WSO2 ESB简单介绍下吧. 前提: 一切文档,知识都要与官方文档为准. WSO2 ESB: http://wso2.com/products/ ...
- stretchlim函数分析
在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下 function lowhigh = stretchlim(varargin) %STRETCHLIM Find ...
- 混合高斯模型和EM算法
这篇讨论使用期望最大化算法(Expectation-Maximization)来进行密度估计(density estimation). 与k-means一样,给定的训练样本是,我们将隐含类别标签用表示 ...
- redis学习研究--基础知识
以下内容多为摘抄转载: 1. Redis 是什么 Redis是一个开源的使用ANSI C语言编写的基于内存的key/value存储系统,与memcache类似,但它支持的value类型更多,包括:字符 ...
- java设计模式之Proxy(代理模式)
java设计模式之Proxy(代理模式) 2008-03-25 20:30 227人阅读 评论(0) 收藏 举报 设计模式javaauthorizationpermissionsstringclass ...
- werkzeug源码阅读笔记(二) 上
因为第一部分是关于初始化的部分的,我就没有发布出来~ wsgi.py----第一部分 在分析这个模块之前, 需要了解一下WSGI, 大致了解了之后再继续~ get_current_url()函数 很明 ...
- 使用PHP从web访问mysql数据库
一. web数据库构架的工作原理 1. 用户由浏览器发出HTTP请求,请求特定的web页面. 2. web服务器接受接收到对特定页面的请求,检索相应文件,并将其传递给php引擎处理. 3. php引擎 ...
- 007.androidUI开发进阶(基础--案例) .
1.Dialog有四种,分别是AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 1.1AlertDialog public cl ...
- 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总
习·D3.js 学习资源汇总 除了D3.js自身以外,许多可视化工具包都是基于D3开发的,所以对D3的学习就显得很重要了,当然如果已经有了Javascript的经验,学起来也会不费力些. Github ...