[LC] 116. Populating Next Right Pointers in Each Node
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.
Example:

Input: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}
Output: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.
Note:
- You may only use constant extra space.
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next; public Node() {} 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) {
if (root == null) {
return null;
} if (root.left != null) {
root.left.next = root.right;
if (root.next != null) {
root.right.next = root.next.left;
}
}
connect(root.left);
connect(root.right);
return root;
}
}
[LC] 116. Populating Next Right Pointers in Each Node的更多相关文章
- 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] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- LeetCode OJ 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...
- 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
随机推荐
- 第二季 第四集 part3
obj.insertAdjancetHtlm("beforeend"(位置), r(内容)) insertAdjacentHTML() 将指定的文本解析为HTML或XML,并将结果 ...
- mysql增删查改
<?php $db_host = 'localhost:3306'; $db_user = 'root'; $db_passwd = ''; $db_na ...
- Innodb-内存架构与特性
参考文档 Innodb特性buffer_pool http://mysql.taobao.org/monthly/2017/05/01/?spm=a2c4e.11153940.blogcont2812 ...
- Pmw大控件
Python大控件——Pmw——是合成的控件,以Tkinter控件为基类,是完全在Python内写的.它们可以很方便地增加功能性的应用,而不必写一堆代码.特别是,组合框和内部确认计划的输入字段放在一起 ...
- python编程:从入门到实践----第五章:if语句>练习
5-1 条件测试 :编写一系列条件测试:将每个测试以及你对其结果的预测和实际结果都打印出来. a. 详细研究实际结果,直到你明白了它为何为True 或False b. 创建至少2个测试,且其中结果分别 ...
- Python cannot import name 'Line' from 'pyecharts'
问题与尝试 代码 from pyecharts.charts import Line 中,出现 cannot import name 'Line' from 'pyecharts' 错误. 找了很多, ...
- Maven--仓库的分类
对于 Maven 仓库来说,仓库只分为两类:本地仓库和远程仓库. 当 Maven 根据坐标寻找构件的时候,它首先会查看本地仓库,如果本地仓库存在此构件,则直接使用:如果本地仓库不存在此构件,或者需要查 ...
- 可视化---seaborn
变量说明 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整数 estimator 在每个分类中进 ...
- 让几个横向排列的浮动子div居中显示的方法
div设置成float之后,就无法使子div居中显示了,那么如何让几个横向排列的浮动的div居中显示呢,下面有个不错的方法,希望对大家有所帮助 div设置成float之后,在父div中设置text-a ...
- input标签添加上disable属性在移动端(ios)字体颜色及边框颜色不兼容的解决办法。
手机一些兼容性问题: 1.苹果手机输入框input:disabled显示模糊问题 input:disabled, input[disabled]{ color: #5c5c5c; -webkit-te ...