Leetcode: Populating Next Right Pointers in Each Node 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

在Populating Next Right Pointers in Each Node问题的基础上,难度20,方法一样。都是类似Binary Tree Level Order Traverse,都是把树看成一个无向图,然后用BFS的方式,需要记录每一层的ParentNumInQueue以及ChildNumInQueue, 初始值为1和0,以后每次ParentNumInQ减至0说明这一层已经遍历完毕,这一层的Child数将成为下一层的ParentNumInQ

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * public class TreeLinkNode {
 4  *     int val;
 5  *     TreeLinkNode left, right, next;
 6  *     TreeLinkNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public void connect(TreeLinkNode root) {
11         if (root == null) return;
12         LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
13         queue.add(root);
14         int ParentNumInQ = 1;
15         int ChildNumInQ = 0;
16         TreeLinkNode pre = null;
17         while (!queue.isEmpty()) {
18             TreeLinkNode cur = queue.poll();
19             ParentNumInQ--;
20             if (pre == null) {
21                 pre = cur;
22             }
23             else {
24                 pre.next = cur;
25                 pre = pre.next;
26             }
27             if (cur.left != null) {
28                 queue.add(cur.left);
29                 ChildNumInQ++;
30             }
31             if (cur.right != null) {
32                 queue.add(cur.right);
33                 ChildNumInQ++;
34             }
35             if (ParentNumInQ == 0) {
36                 ParentNumInQ = ChildNumInQ;
37                 ChildNumInQ = 0;
38                 pre.next = null;
39                 pre = null;
40             }
41         }
42     }
43 }

 

 

层次递进法

复杂度

时间 O(N) 空间 O(1)

 1 public class Solution {
 2     
 3     //based on level order traversal
 4     public void connect(TreeLinkNode root) {
 5 
 6         TreeLinkNode head = null; //head of the next level
 7         TreeLinkNode prev = null; //the leading node on the next level
 8         TreeLinkNode cur = root;  //current node of current level
 9 
10         while (cur != null) {
11             
12             while (cur != null) { //iterate on the current level
13                 //left child
14                 if (cur.left != null) {
15                     if (prev != null) {
16                         prev.next = cur.left;
17                     } else {
18                         head = cur.left;
19                     }
20                     prev = cur.left;
21                 }
22                 //right child
23                 if (cur.right != null) {
24                     if (prev != null) {
25                         prev.next = cur.right;
26                     } else {
27                         head = cur.right;
28                     }
29                     prev = cur.right;
30                 }
31                 //move to next node
32                 cur = cur.next;
33             }
34             
35             //move to next level
36             cur = head;
37             head = null;
38             prev = null;
39         }
40         
41     }
42 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/3978460.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399028.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入理解javascript原型和闭包(4)——隐式原型

注意&#xff1a;本文不是javascript基础教程&#xff0c;如果你没有接触过原型的基本知识&#xff0c;应该先去了解一下&#xff0c;推荐看《javascript高级程序设计&#xff08;第三版&#xff09;》第6章&#xff1a;面向对象的程序设计。 上节已经提到&#xff0c;每个函数…

ecshop 手机版的php代码在哪里,PHP 在ecshop上集成 手机网页支付_php

参考alipay网页支付接口的代码其实原理跟ecshop上集成的alipay支付差不多 就是因为利用curl请求的时候相应时间过长 所以不能直接去先post数据再生成button/*** 生成支付代码* param array $order 订单信息* param array $payment 支付方式信息*/function get…

技术回归本位:海尔引领空调产业重构格局

当前&#xff0c;互联网新思维方式日趋侵染&#xff0c;越来越多的细分领域在“互联网”理念下纷纷尝试跨界探索新的创新&#xff0c;一些商家除了推出全新战略型产品和服务之外&#xff0c;还在主打营销概念争夺舆论风口方面投入了巨大的精力与资源。在这种以理念为中心的时代…

护肤

选择什么 护肤品 2222选择什么 1氨基酸洗面奶&#xff1a;去油控油能力适中&#xff0c;用完皮肤清爽&#xff0c;比较亲和&#xff0c;一般成分里多次出现“氨酸”这两个字的就是氨基酸洗面奶&#xff0c;这种洗面奶适合长期使用.</p><p><b>皀基洗面奶&…

与TCP/IP协议的初次见面(一)

引言 最近LZ有了一点时间&#xff0c;于是便拿出TCP/IP的书本开始啃。开始的时候&#xff0c;啃起来枯燥无味&#xff0c;现在好不容易有点开窍&#xff0c;于是赶忙记录一下&#xff0c;生怕自己一转眼就给忘了。不过计算机系统原理就有点可惜了&#xff0c;最近一直没时间看&…

Oracle约数,Oracle约束简介

整理自《OCP认证指南》001 概述表约束是数据库能够实施业务规则以及保证数据遵循实体——关系模型的一种手段&#xff0c;其中&#xff0c;实体——关系模型由定义应用程序数据结构的系统分析所确定。在针对定义了约束的表执行任何DML时&#xff0c;如果DML违反了约束&#xff…

用cmd运行java可以javac不行(win10)

今天发现个有趣的问题&#xff0c;用cmd运行java可以javac不行。(win10) java-home和classpath配置没有问题,最后发现问提出先在path&#xff0c;在这里看并没有异常。 在上面图片中点击编辑文本&#xff0c;在这里可以清楚的看见多了引号和分号&#xff0c;将其删除&#xff0…

vs窗体 oracle,VS2010连接oracle数据库的简单例子

下面附有代码&#xff1a;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OracleClient;namespace 连接oracle数据库…

EasyUI实现两个列表联动

开发中会遇到如下界面的功能样式&#xff1a; 点击左边列表记录时&#xff0c;右边的列表显示所属分类的数据 实现方法&#xff1a; 1、首先绑定左侧列表的OnClickRow事件&#xff0c;方法为&#xff1a;getDetail. 如下代码所示。 <table id"dg" class"easy…

CodeForces 176A Trading Business 贪心

Trading Business题目连接&#xff1a; http://codeforces.com/problemset/problem/176/A Description To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anythi…

Docker快速入门实践-纯干货文章

Docker快速入门实践-老男孩高级架构师课程内容&#xff0c;如果细看还能发现讲解视频呦&#xff01;小伙伴们赶紧猛戳吧&#xff01;老男孩高级架构师内部学员实践文档分享&#xff01;Docker快速入门实践-纯干货文章老男孩教育2016启用最新的官方博文地址&#xff1a;http://b…

180102

https://pan.baidu.com/s/1nvqYFt3转载于:https://www.cnblogs.com/wjy123/p/8175593.html

oracle的iw算法,[转载]Oracle日期周详解IW

1 ORACLE中周相关知识描述1.1 日期格式化函数TO_CHAR(X [,FORMAT])&#xff1a;将X按FORMAT格式转换成字符串。X是一个日期&#xff0c;FORMAT是一个规定了X采用何种格式转换的格式字符串&#xff0c;FORMAT与周相关的有W&#xff0c;WW&#xff0c;IW&#xff0c;D&…

spring3 常见异常解决

初学spring&#xff0c;在网上搜到一篇spring3常见异常的文章&#xff0c;现转载如下&#xff1a; 以下异常使用的是spring是3.1.1&#xff0c;是我自己学习中遇到的错误笔记&#xff0c;有可能不是都适用&#xff0c;仅做参考 异常1&#xff1a;java.lang.NoClassDefFoundErro…

git忽略某个文件夹

data/cache/* !data/cache/index.html !data/cache/smiOAuthToken.php 转载于:https://www.cnblogs.com/xiaobiaomei/p/8177168.html

Iterm2的一些好用法

今天把mac带到公司办公了&#xff0c;爽歪歪啊。 1&#xff0c;如何脱离鼠标拷贝屏幕中的内容 1) Commandf 调出选择框&#xff0c;并在其中输入要复制的字符&#xff0c;可以使用Tab补全 2) 按 Command c复制字符 3) 字符已经复制到剪切板了

记录一些容易忘记的属性 -- UITabBarController

UIViewController中的 property(nonatomic,copy) NSString *title; // Localized title for use by a parent controller.&#xff0c;仔细理解英文注释的意思 下面是Title的实际作用 //创建给分栏控制器使用的视图控制器(包括导航控制器) FirstViewController *firstVC …

LINQ to Tree - A Generic Technique for Querying Tree-like Structures,包含遍历WPF VisualTree

https://www.codeproject.com/Articles/62397/LINQ-to-Tree-A-Generic-Technique-for-Querying-Tree#generic 转载于:https://www.cnblogs.com/sjqq/p/8177276.html

在 ASP.NET MVC 3 中应用 KindEditor

http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中&#xff0c;建议放到 /Scripts/kindeditor 目录中&#xff0c;其中只需要有 lang目录、plugis目录、themes目录和kindeditor-min.js文件即可。 第二步 在 /Views/S…

php phalcon.dll 下载,extension=php_phalcon.dll 安装过程

到phalcon官方网站下载对应的dll文件 phalcon_x86_VC9_php5.4.0_1.2.5 我下的是这个版本 所以用的wamp版本的php也是 php 5.4版本&#xff1b;所以没啥问题&#xff01;3&#xff1a;下载完了dll文件放在 刚安装好的wamp服务器下&#xff1a;因为我安装的是D盘&#xff1a;D:\w…