LeetCode_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
方法一: constant 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( !root || ( !root->left && !root->right)) return ; root->left->next = root-> right;
if(root->next)
root->right->next = root->next->left;
connect(root->left);
connect(root->right); }
};
方法二: 空间使用上貌似不符合要求
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root) return ;
queue<TreeLinkNode *> myqueue, tpqueue;
TreeLinkNode *p,*pre;
myqueue.push(root);
while(!myqueue.empty()){
pre = myqueue.front();myqueue.pop();
if(pre->left) tpqueue.push(pre->left);
if(pre->right) tpqueue.push(pre->right);
while(!myqueue.empty()){
p = myqueue.front();myqueue.pop();
if(p->left) tpqueue.push(p->left);
if(p->right) tpqueue.push(p->right);
pre->next = p;pre = p;
} myqueue.swap(tpqueue);
}
}
};
方法三: 方法一的非递归实现,符合题目的空间要求
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root) return;
TreeLinkNode *head = root;
TreeLinkNode *tp;
while(head->left){
tp = head->left;
while(head){
if(head->left)
head->left->next = head ->right;
if(head ->right && head->next)
head->right->next = head->next->left;
head = head ->next;
}
head = tp;
}
}
};
LeetCode_Populating Next Right Pointers in Each Node的更多相关文章
- 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 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [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
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 II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
随机推荐
- 自制单片机之十五……可串行驱动LCD12864的应用
在网上搜了一下,ST7920控制器的LCD产品可以提供8位,4位并行和串行接口可选,并行的控制接口的LCD较多,前面的贴子也介绍过,我们在这儿不说了,这儿我们讲的是串口控制LCD12864. 买了块S ...
- PowerShell 简单模式识别 1
PowerShell 简单模式识别 1 10 6月, 2013 在 Powershell tagged 字符串 / 文本 / 通配符 by Mooser Lee 在验证用户的条目时,模式识别是必要并 ...
- sql的交叉连接,内连接,左外连接,右外连接,全外连接总结
实践是最好的检验,一直都对这几个连接查询出来的结果有什么不同不大理解,然后自己放一块查询比较了一下,用结果来说话~ 先建两张表如下: t1: id name age 1 张三 18 2 李四 25 t ...
- Abstract Methods and Classes
阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...
- POJ2367 Genealogical tree (拓扑排序)
裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...
- MergeSort 归并排序
实现: 二路归并 public class TestMergeSort { public int[] mergeSortArray(int[] arr, int left, int right){ i ...
- pkill killall kill pidof
http://blog.csdn.net/whycold/article/details/11771841 http://www.cnblogs.com/rsky/p/4886043.html ht ...
- 运行tomcat7w.exe tomcat7.exe ,提示 指定的服务未安装 unable to open the service 'tomcat7'
运行tomcat7w.exe tomcat7.exe ,提示 指定的服务未安装 unable to open the service 'tomcat7'(用的是绿色的Tomcat7) 解决方法: 打开 ...
- android 4.0之前版本号出现JSONException异常
今天在调试解析server传过来的JSON数据时,在2.3.7的手机上报了以下这样一个异常. 08-07 22:00:29.597: W/System.err(7610): org.json.JSON ...
- python查看删除你微信的账号
#应用环境:python2.7 #!/usr/bin/env python # coding=utf-8 from __future__ import print_function import os ...