【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 ...
随机推荐
- nyoj 289 苹果 动态规划 (java)
分析:0-1背包问题 第一次写了一大串, 时间:576 内存:4152 看了牛的代码后,恍然大悟:看来我现在还正处于鸟的阶段! 第一次代码: #include<stdio.h> #inc ...
- mybatis 多个dao重名,根据namespace解析
在mybatis通过执行sql语句的方式是,用getSqlSession().xxx(param,..)方法来调用, 其中第一个参数就是dao mapper.xml文件的命名空间.id package ...
- MySQL数据库的事务管理
当前在开发ERP系统,使用到的数据库为Mysql.下面介绍下如何开启事务,以及事务隔离的机制 : 1. 检查当前数据库使用的存储引擎. show engines; 2. 修改前my.ini中的文件如下 ...
- 【C语言入门教程】4.6 指针 和 数组
数组在内存中以顺序的形式存放,数组的第一个存储单元的地址即数组的首地址.对一维数组来说,直接引用数组名就能获得该数组的首地址.指针变量可以存放于其内容相同的数组首地址,也可以指向某一具体的数组元素.通 ...
- Javascript高级程序设计——在HTML中使用Javascript
<script>元素 向HTML页面中插入Javascript的主要方法,就是使用<script>元素,<script>元素有六个属性: async:可选.表示应该 ...
- 简单实现div遮罩
顾名思义,div遮罩就是将网页上的一部分用div遮盖起来,防止用户误点,因此div遮罩的一个用途就是将table设置为不可编辑. 作者通过查找资料,并进行简单的测试,最终完成了以下几段简单代码,来实现 ...
- 实战CentOS系统部署Hadoop集群服务
导读 Hadoop是一个由Apache基金会所开发的分布式系统基础架构,Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高 ...
- POJ 2631 DFS+带权无向图最长路径
http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...
- 代码高亮插件SyntaxHighlighter
http://alexgorbatchev.com/SyntaxHighlighter/download/
- IDEA 回滚SVN更新内容