【leetcode】Populating Next Right Pointers in Each Node II
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
只是当我们没有找到时,father=father->next;
/**
* 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;
} if(root->left!=NULL)
{
if(root->right!=NULL)
{
root->left->next=root->right;
}
else
{
TreeLinkNode *tmp=root->next;
root->left->next=NULL;
while(tmp!=NULL)
{
if(tmp->left!=NULL)
{
root->left->next=tmp->left;
break;
} if(tmp->right!=NULL)
{
root->left->next=tmp->right;
break;
}
tmp=tmp->next;
}
}
} if(root->right!=NULL)
{
TreeLinkNode *tmp=root->next;
root->right->next=NULL;
while(tmp!=NULL)
{
if(tmp->left!=NULL)
{
root->right->next=tmp->left;
break;
} if(tmp->right!=NULL)
{
root->right->next=tmp->right;
break;
}
tmp=tmp->next;
}
} connect(root->right);
connect(root->left); }
};
【leetcode】Populating Next Right Pointers in Each Node II的更多相关文章
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [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 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- 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 ...
随机推荐
- python中os/sys/platform模块区别
os:This module provides a portable way of using operating system dependent functionality. sys:This m ...
- OC-点语法
点语法的本质:方法调用 #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...
- yii2.0自动登录功能的实现方法
参考地址:http://www.kuitao8.com/20150518/3747.shtml 自动登录的原理很简单.主要就是利用cookie来实现的在第一次登录的时候,如果登录成功并且选中了下次自动 ...
- Java多线程编程核心技术--Lock的使用(一)
使用ReentrantLock类 在Java多线程中,可以使用synchronized关键字来实现线程之间的同步互斥,但在JDK1.5中新增加了ReentrantLock类也能达到同样的效果,并且在扩 ...
- CentOs图形界面的开启与关闭
1.1 shell中运行 init 3 进入文本模式,同时会关闭相关的服务(Xserver 肯定关闭) 1.2 Alt+Ctrl+F1~F6到字符界面,root登陆,ps aux|grep /usr ...
- JAVA利用Zip4j解压缩【转】
官方地址:http://www.lingala.net/zip4j/(需要FQ) jar包:http://pan.baidu.com/s/145hwI 演示包:http://pan.baidu.com ...
- 黄永成-thinkphp讲解-个人博客讲解26集
如果是要导入 前后台分组都公用的 第三方类库, 就将类库(*.class.php文件) 放在 App下的Class文件夹中. 导入方法是: import('Class.Image', APP_PATH ...
- 大熊君大话NodeJS之------Global Objects全局对象
一,开篇分析 在上个章节中我们学习了NodeJS的基础理论知识,对于这些理论知识来说理解是至关重要的,在后续的章节中,我们会对照着官方文档逐步学习里面的各部分模块,好了该是本文主角登台亮相的时候了,G ...
- [设计模式] javascript 之 享元模式;
享元模式说明 定义:用于解决一个系统大量细粒度对象的共享问题: 关健词:分离跟共享: 说明: 享元模式分单纯(共享)享元模式,以及组合(不共享)享元模式,有共享跟不共享之分:单纯享元模式,只包含共享的 ...
- 跨域攻击xss
要完全防止跨域攻击是很难的,对于有些站点是直接 拦截跨域的访问,在你从本站点跳转到其他站点时提醒,这算是一种手段吧. 而跨域攻击的发起就是在代码(包括html,css,js或是后台代码,数据库数据)里 ...