LeetCode Populating Next Right Pointers in Each Node (技巧)
题意:
给一棵满二叉树,要求将每层的节点从左到右用next指针连起来,层尾指向NULL即可。
思路:
可以递归也可以迭代。需要观察到next的左孩子恰好就是本节点的右孩子的next啦。
(1)递归:这个更快。
/**
* 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* root,TreeLinkNode* t)
{
if(root->next==NULL && t!=NULL) root->next=t->left;
if(root->left)
{
root->left->next=root->right;
DFS(root->left, NULL);
DFS(root->right,root->next);
}
}
void connect(TreeLinkNode *root) {
if(root) DFS(root,NULL);
}
};
AC代码
(2)递归:这个简洁。
/**
* 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 && root->left)
{
root->left->next=root->right;
if(root->next)
root->right->next=root->next->left;
connect(root->left);
connect(root->right);
} }
};
AC代码
(3)迭代:这个更吊。
/**
* 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) {
while(root&&root->left)
{
TreeLinkNode *p=root;
while(p)
{
p->left->next=p->right;
if(p->next)
p->right->next=p->next->left;
p=p->next;
}
root=root->left;
}
}
};
AC代码
LeetCode Populating Next Right Pointers in Each Node (技巧)的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [LeetCode] 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] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode——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]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 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 II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- Sumblime Text2安装Package Control两种方法+安装插件+注册码
刚开始不认识sumblime的时候,就直接在网上下载了一个最新版的sumblime text3,只是在配合使用go语言时,出现了一些不为自己知道的奇葩问题,于是果断把3卸载了,改成了sumblime ...
- GO工程和包
转载:http://blog.csdn.net/achelloworld/article/details/43956831 在Go语言中,包是函数与数据的集合,用package定义一个包,包的名字不一 ...
- 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去
答案:在服务器上操作,注意勾选和不勾选的区别,使用Fiddle进行观察,301和302之间的区别
- 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器
Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...
- 解析网络json数据,模拟美团界面显示。
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...
- JAVA与指针
首先,提个问题:JAVA中没有指针,JAVA中有指针,哪个一个对呢? 答:都对,JAVA中没有指针,因为我们不能对指针直接操作,像C++那样用->来访问变量. JAVA有指针,因为JDK中封装了 ...
- HDU 3074 Multiply game(线段树)
单点更新,更新时先除去 原来的数,因为有去摸,可以用乘上逆元代替. //================================================================ ...
- 让webapi只接受ajax请求
为了测试先做一个简单的webapi,直接用新建项目时默认的就可以了. 在浏览器中测试request get,得到结果 然后再项目中新建一个AjaxOnly的类 AjaxOnly继承Acti ...
- Cisco IOS Debug Command Reference I through L
debug iapp through debug ip ftp debug iapp : to begin debugging of IAPP operations(in privileged EXE ...
- Jquery实现的Tabs标签页
效果图: HTML: <div class="tabs"> <ul id="tabs"> <li class="tab- ...