[LC] 117. Populating Next Right Pointers in Each Node II
Given a binary tree
struct Node {
int val;
Node *left;
Node *right;
Node *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.
Follow up:
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
Example 1:
Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next; public Node() {} public Node(int _val) {
val = _val;
} public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
Node cur = root;
Node prev = null;
Node head = null;
while (cur != null) {
while (cur != null) {
if (cur.left != null) {
if (prev != null) {
prev.next = cur.left;
} else {
head = cur.left;
}
prev = cur.left;
}
if (cur.right != null) {
if (prev != null) {
prev.next = cur.right;
} else {
head = cur.right;
}
prev = cur.right;
}
cur = cur.next;
}
cur = head;
head = null;
prev = null;
}
return root;
}
}
[LC] 117. Populating Next Right Pointers in Each Node II的更多相关文章
- 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】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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】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 117 Populating Next Right Pointers in Each Node II ----- java
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 117. Populating Next Right Pointers in Each Node II (Tree; WFS)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- python基础1--基本数据类型+流程控制
一.基本数据类型 1.整型 int 就是整数 2.浮点型 float 就是小数 3.字符串 3.1.加了单引号.双引号.多引号的字符就认为是字符串 单引号和双引号没有什么区别,多引号用 ...
- tfield的字段名和显示名
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- cmd 进入指定文件夹
1.通常情况下,我们要进入其他盘符下的任意目录,需要在CMD窗口运行两次命令:第一次,进入盘符,第二次进入指定目录 #进入D盘 d: #进入D盘下的anaconda目录 cd anacond 2.通过 ...
- zabbix中文乱码解决
问题现象: zabbix字体修改成中文后监控显示乱码 原因: 该问题是由于zabbix默认使用的是“DejaVuSans.ttf”(zabbix3.2.7默认使用的是“graphfont.ttf”), ...
- alert(1&&2)的输出问题
此文章转载自http://blog.csdn.net/xiaotao_css/article/details/61940664 一.先来说说 ||(逻辑或),从字面上来说,只有前后都是false的时候 ...
- MVC——再探MVC——增删查改
MVC 是我大学学的比较弱的,甚至不懂原理.(那时候都在准备蓝桥杯 软件杯比赛.) 在重新学 肯定要学MVC 现在知道了为什么叫MVC了 MVC是怎么工作的 MVC 是一个设计模式 控制器(Cont ...
- 项目常用JS方法封装--奋斗的IT青年(微信公众号)
...
- document.write的时机
document.write第一次加载进入页面的时候会紧跟文档,写入内容.但是如果在文档已经加载完毕之后,再通过点击的方式调用函数的话会直接把整个文档覆盖掉.
- Ubuntu16装Flash
第一种方法: 1)下载flash的tar.gz压缩包.(以·tar.gz为后缀的文件是一种压缩文件,在Linux和macOS下常见,Linux和macOS都可以直接解压使用这种压缩文件) https: ...
- Properties类(一)
Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...