Leetcode-Populating Next Right Pointer in Binary Tree II
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
Solution:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if (root==null)
return; root.next = null;
TreeLinkNode levelHead = root; while (true){
boolean hasChild = false;
TreeLinkNode cur = levelHead;
TreeLinkNode nextLevelPre = null;
TreeLinkNode nextLevelHead = null;
while (cur!=null){
if (cur.left!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.left;
nextLevelPre = cur.left;
} else {
nextLevelPre = cur.left;
nextLevelHead = cur.left;
}
} if (cur.right!=null){
hasChild = true;
if (nextLevelPre!=null){
nextLevelPre.next = cur.right;
nextLevelPre = cur.right;
} else {
nextLevelPre = cur.right;
nextLevelHead = cur.right;
}
} cur = cur.next;
}
if (hasChild)
nextLevelPre.next = null; //If reach the last level, then stop.
if (!hasChild)
break; //Move to next level.
levelHead = nextLevelHead;
} return;
}
}
At each level, after we construct the link list, we have a linked list to visit all nodes in this level. Then we can visit all child nodes in the next level along this linked list.This is a very smart way.
Note: For this question, we need to be careful about where the first child node in the next level is.
Leetcode-Populating Next Right Pointer in Binary Tree II的更多相关文章
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- LeetCode: Populating Next Right Pointer in Each Node
LeetCode: Populating Next Right Pointer in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- LeetCode之104. Maximum Depth of Binary Tree
-------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...
随机推荐
- 删除Git记录里的大文件
删除Git记录里的大文件 仓库自身的增长 大多数版本控制系统存储的是一组初始文件,以及每个文件随着时间的演进而逐步积累起来的差异:而 Git 则会把文件的每一个差异化版本都记录在案.这意味着,即使你只 ...
- Wpf 自定义控件(1)
1. 新建一个wpf工程,在工程下面新建 一个文件夹themes,在themes下新建两个资源字典文件generic.xaml和PrettySeekBar.xaml generic.xaml ...
- SiteWhere物联网云平台架构
SystemArchitecture系统架构 Thisdocument describes the components that make up SiteWhere and how theyrela ...
- 点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,
效果: 思路: 首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库. 数据库: create table image1 ( ID ,) primary key, Im ...
- numpy.meshgrid()理解
本文的目的是记录meshgrid()的理解过程: step1. 通过一个示例引入创建网格点矩阵; step2. 基于步骤1,说明meshgrid()的作用; step3. 详细解读meshgrid() ...
- linux学习笔记29---命令watch
watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...
- Android 开发 -------- 自己定义View 画 五子棋
自己定义View 实现 五子棋 配图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG92ZV9KYXZjX3lvdQ==/font/5a6L5L2T ...
- C. Beautiful Numbers
C. Beautiful Numbers Time Limit : 4000/2000ms (Java/Other) Memory Limit : 524288/262144K (Java/Oth ...
- dva学习---effects异步中通过select获取当前的state
根据 在组件中dispatch一个action的例子中,如果要在effects中对于param数据和当前的state数据进行再出处理,这里怎么获取state呢?采用select,如下: e ...
- beautifulSoup安装
Python2.7 + beautifulSoup 4.4.1 安装配置 原创 2016年05月09日 10:20:30 标签: python 1261 1. 前言 最近研究python 的爬虫功能, ...