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 ...
随机推荐
- Ruby中,类方法和实例方法的一个有趣的例子
最初的代码如下: class Object def abc p "instance abc" end def self.abc p "class abc" en ...
- [Angular 2] Using the @Inject decorator
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- Android高级图片滚动控件,编写3D版的图片轮播器
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17482089 大家好,好久不见了,最近由于工作特别繁忙,已经有一个多月的时间没写博 ...
- openssl 摘要和签名验证指令dgst使用详解
1.信息摘要和数字签名概述 信息摘要:对数据进行处理,得到一段固定长度的结果,其特点输入: 1.输出长度固定.即输出长度和输入长度无关. 2.不可逆.即由输出数据理论上不能推导出输入数据 4.对输入数 ...
- for循环和经典案例
循环:初始条件,循环条件,状态改变,循环体.for(初始条件;循环条件;状态改变){ 循环体}for(int i=1;i<=10;i++){ }例子:100以内与7有关的数.求100以内所有数的 ...
- js Date扩展Format()函数
Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', ...
- (转).net程序员转战android第二篇---牛刀小试
上篇说道如何搭建android的开发环境,这一篇我们将牛刀小试一下, 完成我们第一个android APP应用. 我就从新建项目说起吧. 首先打开Eclipse,选择顶部的File(文件)——new( ...
- Codeforces Round #350 (Div. 2)A,B,C,D1
A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 内存管理pbuf.c源码解析——LwIP学习
声明:个人所写所有博客均为自己在学习中的记录与感想,或为在学习中总结他人学习成果,但因本人才疏学浅,如果大家在阅读过程中发现错误,欢迎大家指正. 本文自己尚有认为写的不完整的地方,源代码没有完全理清, ...
- 洛谷 P1066 2^k进制数
P1066 2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. ( ...