【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 ...
随机推荐
- app接口的简单案例 和一些总结
例一: 通过接口获取一篇文章.接口需要传入文章的id,通过sql语句向数据库查询文章的内容,然后以json的格式echo出即可,即:安卓或IOS工程师获取通过接口获取到了json格式的数据,在做进一步 ...
- jsonp解决CORS问题
jsonp是个机智的解决办法: 1.本地页面写个js方法 <script> function abc(data) { alert(data.result); } </script&g ...
- mysql支持跨表delete删除多表记录
前几天写了Mysql跨表更新的一篇总结,今天我们看下跨表删除. 在Mysql4.0之后,mysql开始支持跨表delete. Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关 ...
- 浙大PAT-1001
1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...
- R笔记 map_leaflet googlevis
packages : map leaflet library(leaflet) library(maps) mapStates = map("state", fill = TRUE ...
- tomcat中配置jmx监控
1.在tomcat的start.bat中添加下面代码, -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote. ...
- 为在韶大痛苦而不能用手机、Pad等上网的同志造福!
目标:共享咱们校园网,让更多的人或更多的设备冲浪去! 基本条件:一台带无线功能的笔记本,一个可以上网的账号与pwd,最好为Windows7以上的操作系统,如果是XP,则需要打个.net framewo ...
- 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理
导读 Nginx 是最快和最强大的 Web 服务器之一,以其高性能和低资源占用率而闻名.它既可以被安装为一个独立的 Web 服务器,也可以安装成反向代理 Web 服务器.在这篇文章,我将讨论在安装了 ...
- 据说Linuxer都难忘的25个画面
导读 虽然对 Linux 正式生日是哪天还有些争论,甚至 Linus Torvalds 认为在 1991 那一年有四个日子都可以算作 Linux 的生日.但是不管怎么说,Linux 已经 25 岁了, ...
- ios7 ios8导航栏透明
自动调整scrollview的insets为0, 然后scrollview就不会向下偏移64px self.automaticallyAdjustsScrollViewInsets = NO; 导航栏 ...