【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
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
思路:一层一层的连接,同一个父节点的左子树的邻居是其父节点的右子树 父节点右子树的邻居是 父节点邻居的左子树
我用的递归
void connect(TreeLinkNode *root) {
if(root == NULL) return;
TreeLinkNode * parent = root;
TreeLinkNode * cur = parent->left;
while(parent != NULL && cur != NULL)
{
cur = cur->next = parent->right; //左子树的邻居是同一个parent的右子树
cur->next = (parent->next == NULL) ? NULL : parent->next->left; //右子树的邻居 是parent的邻居的左子树
parent = parent->next;
cur = (parent == NULL) ? NULL : parent->left;
}
connect(root->left); //连接下一层
}
大神的非递归代码:外面加一圈对层循环
void connect(TreeLinkNode *root) {
if(!root)
return;
while(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;
}
}
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
思路:非完全二叉树。关键是每一层要判断邻居是哪一个,每层的第一个有数字的位置也要定位。
我的代码,根据上一题的思路,用的非递归。对父节点左右子树都空,左子树空,右子树空,都非空分类处理。
void connect2(TreeLinkNode *root) {
if(root == NULL) return;
TreeLinkNode * newroot = root;
while(newroot != NULL) //对层循环
{
TreeLinkNode * p = newroot; //当前层父节点推进
TreeLinkNode * cur = NULL; //当前连接层当前指针位置
newroot = NULL; //下一层第一个父节点的位置
while(p != NULL)
{
if(p->left == NULL && p->right == NULL);
else if(p->left == NULL)
{
if(cur == NULL)
newroot = cur = p->right;
else
cur = cur->next = p->right;
}
else if(p->right == NULL)
{
if(cur == NULL)
newroot = cur = p->left;
else
cur = cur->next = p->left;
}
else
{
if(cur == NULL)
newroot = cur = p->left;
else
cur = cur->next = p->left; cur = cur->next = p->right;
}
p = p->next;
}
}
}
大神的代码,处理的时候只要分左子树是否空,和右子树是否空即可。不需要分那么多情况。
public class Solution {
public void connect(TreeLinkNode root) { while(root != null){
TreeLinkNode tempChild = new TreeLinkNode(); //该层的伪头结点,方便定位下一层第一个值的位置
TreeLinkNode currentChild = tempChild; //这两个值不用每次分配,在外面分配,内部循环使用即可
while(root!=null){ //只分两种情况就行了
if(root.left != null) { currentChild.next = root.left; currentChild = currentChild.next;}
if(root.right != null) { currentChild.next = root.right; currentChild = currentChild.next;}
root = root.next;
}
root = tempChild.next;
}
}
}
【leetcode】Populating Next Right Pointers in Each Node I & II(middle)的更多相关文章
- 【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 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 * ...
- 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【Leetcode】【Medium】Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
随机推荐
- HTML中head头结构
HTML head 头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO等等,而各个浏览器内核以及各个国内浏览器厂商都有些自己的标签元素,这就造成了很多差异性.移动互联网时代,head 头部结构 ...
- [译]git reset
git reset 如果说git revert是一个安全的撤销方式, 那么git reset就是一个非常危险的方法了. 当你使用git reset撤销的时候, 你没有可能在回到最初了-他是一个永久的不 ...
- VTK初学一,b_PolyVertex_CellArray多个点的绘制
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- SpringMVC的Controller方法的参数不能直接绑定List、Set、Map
List需要绑定在对象上,而不能直接写在Controller方法的参数中. http://www.iteye.com/topic/973918
- 串行移位锁存并行输出可级联器件74HC595
一.背景 老同学今天突然咨询关于74HC595,自己没用过,同学说可以级联10级!10级?我艹,这么叼,级联又是 什么鬼,这勾起了我极大兴趣,二话不说,手册down下来研究,并在此做个记录. 二.正文 ...
- ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭)
欧拉函数,用φ(n)表示 欧拉函数是求小于等于n的数中与n互质的数的数目 辣么,怎么求哩?~(-o ̄▽ ̄)-o 可以先在1到n-1中找到与n不互质的数,然后把他们减掉 比如φ(12) 把12质因数分解 ...
- 给一系列的div中的第一个添加class
$(".lb:first").addClass("active")
- 格式化Double类型
//格式化Double类型 //F:默认是2位小数点 //F6:输出小数点后6位,不够的话用0补齐 //G:默认输出原先的,保留小数点后面的位数 LalTotal.Text = "合计:原始 ...
- 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
由于给予REST的Web服务非常简单易用,它越来越成为企业后端服务集成的首选方法.本文这里介绍一下如何通过微软的Asp.Net WebAPI快速构建REST-ful 服务. 首先创建一个Asp.Net ...
- sass跨文件重写变量
利用变量默认值: !default 你可以在变量尚未赋值前,通过在值的末尾处添加 !default 标记来为其指定. 也就是说,如果该变量已经被赋值, 就不会再次赋值, 但是,如果还没有被赋值,就会被 ...