117. Populating Next Right Pointers in Each Node II
2016-07-04 17:24
363 查看
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,
After calling your function, the tree should look like:
题意:如同116题,对任意的二叉树找出其结点的next点。
思路:如同,116题,只是在递归时要先递归右树,然后再递归左树。
/**
* 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) {}
* };
*/
/**
* 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){
if (root->right)
root->left->next = root->right;
else{
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
if (tmp->left){
root->left->next = tmp->left;;
}
else{
root->left->next = tmp->right;
}
break;
}
else
tmp = tmp->next;
}
}
}
if (root->right){
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
if (tmp->left){
root->right->next = tmp->left;;
}
else{
root->right->next = tmp->right;
}
break;
}
else
tmp = tmp->next;
}
}
connect(root->right);
connect(root->left);
}
};
思路2:思路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) {}
* };
*/
/**
* 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;
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
break;
}
else
tmp = tmp->next;
}
TreeLinkNode *t = NULL;
if(tmp){
if (tmp->left)
t = tmp->left;
else
t = tmp->right;
}
if (root->right){
root->right->next = t;
}
if (root->left){
if (root->right)
root->left->next = root->right;
else{
root->left->next = t;
}
}
connect(root->right);
connect(root->left);
}
};
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
题意:如同116题,对任意的二叉树找出其结点的next点。
思路:如同,116题,只是在递归时要先递归右树,然后再递归左树。
/**
* 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) {}
* };
*/
/**
* 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){
if (root->right)
root->left->next = root->right;
else{
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
if (tmp->left){
root->left->next = tmp->left;;
}
else{
root->left->next = tmp->right;
}
break;
}
else
tmp = tmp->next;
}
}
}
if (root->right){
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
if (tmp->left){
root->right->next = tmp->left;;
}
else{
root->right->next = tmp->right;
}
break;
}
else
tmp = tmp->next;
}
}
connect(root->right);
connect(root->left);
}
};
思路2:思路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) {}
* };
*/
/**
* 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;
TreeLinkNode *tmp = root->next;
while (tmp){
if (tmp->left || tmp->right){
break;
}
else
tmp = tmp->next;
}
TreeLinkNode *t = NULL;
if(tmp){
if (tmp->left)
t = tmp->left;
else
t = tmp->right;
}
if (root->right){
root->right->next = t;
}
if (root->left){
if (root->right)
root->left->next = root->right;
else{
root->left->next = t;
}
}
connect(root->right);
connect(root->left);
}
};
相关文章推荐
- nodejs基础概念解析
- window下通过nvmw来安装多版本node
- 116. Populating Next Right Pointers in Each Node
- 分布式存储ceph系统性能不稳定因素
- DrawNode
- Nodejs中 npm常用命令详解
- Nodejs全局安装和本地安装的不同之处
- [算法]Java 实现 简单MyNode类型 无序链表 递归 添加 删除 遍历
- 【原】nodejs全局安装和本地安装的区别
- 24. Swap Nodes in Pairs
- bower安装库出现的问题解决办法
- Node.js Streams文件读写操作详解
- 【web】Ubuntu上安装nodejs 4.x 5.x版本方法
- node 关键点总结
- node.js池操作
- 读《nodejs开发指南》记录
- 【腾讯云】配置安装node
- nodejs开发遇到过的坑
- node.js 和 express 框架学习笔记(3)
- nodejs 循环引用JSON序列化错误的解决办法:TypeError: Converting circular structure to JSON