Populating Next Right Pointers in Each Node II 解答
Question
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
思路与Populating Next Right Pointer 一样,仍是用四个指针 prevHead, prevCurrent, curHead, current层次遍历。
两层循环:
外层循环:traverse level by level
内层循环: traverse last level, link current level
/**
* 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) {
TreeLinkNode prevHead = root, prevCur = root;
TreeLinkNode currentHead = null, current = null;
while (prevHead != null) {
prevCur = prevHead;
// Find current head
while (prevCur != null && prevCur.left == null && prevCur.right == null) {
prevCur = prevCur.next;
}
if (prevCur == null) {
break;
} else if (prevCur.left != null) {
currentHead = prevCur.left;
current = currentHead;
if (prevCur.right != null) {
current.next = prevCur.right;
current = current.next;
}
} else {
currentHead = prevCur.right;
current = currentHead;
}
// link current level
prevCur = prevCur.next;
while (prevCur != null) {
if (prevCur.left != null) {
current.next = prevCur.left;
current = current.next;
}
if (prevCur.right != null) {
current.next = prevCur.right;
current = current.next;
}
prevCur = prevCur.next;
}
// reset
prevHead = currentHead;
} }
}
Populating Next Right Pointers in Each Node II 解答的更多相关文章
- 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】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- 15个提高编程技巧的JavaScript工具
原文地址:http://www.imooc.com/wenda/detail/243523 JavaScript脚本库是一个预先用JavaScript语言写好的库,它方便了我们开发基于JavaScri ...
- Javascript中&&和&,||和|运算符两个不同点
1.性能上的比较 如果&&的第一个运算数是false,就不再考虑第二个运算数,直接返回false:如 果||的第一个运算数是true,也不再考虑第二个运算数,直接返回true.& ...
- PHP中文汉字验证码
hb.ttf换成随便你自己下载的ttf Header("Content-type: image/PNG"); $str="的一是在了不和有大这主中人上为们地个用工时要动国 ...
- 基于PCA的特征提取
图像处理方面的知识也学了一段时间了,总是光看理论的话,感觉联系不上实际,第一次把理论综合的实现出来,对这些理论的印象才感觉的更深刻,也能够为后续的学习打下良好的基础. PCA是比较老的算法,但是可靠性 ...
- poj 1486 Sorting Slides(二分图匹配的查找应用)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- MVC MVC 路由详解
在项目中我们引用了System.Web.Routing; Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...
- gzcompress, gzencode, gzdeflate三个压缩函数的对比
PHP的自带的函数中,有三个压缩相关的函数:gzcompress.gzencode.gzdeflate,下面我们通过一段程序,来比较一下这三个函数的压缩比.代码:$string = "8ae ...
- 退役笔记一#MySQL = lambda sql : sql + ' Source Code 4 Explain Plan '
Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...
- [Javascript] lodash: memoize() to improve the profermence
Link: https://lodash.com/docs#memoize Example: .service('UserPresenter', function(UserConstants){ va ...
- JavaScript 之 call apply bind
关键字 this 绑定的方法 this的动态切换,固然为JavaScript创造了巨大的灵活性,但也使得编程变得困难和模糊.有时,需要把this固定下来,避免出现意想不到的情况.JavaScript提 ...