[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 ...
随机推荐
- Thread--两线程交替打印
package t3.copy; public class ThreadA extends Thread { private Object lock; public ThreadA(Object lo ...
- 第22章 Makefile基础
一.自动处理头文件的依赖关系 在Makefile中插入如下代码: include $(sources:.c=.d) %.d: %.c set -e; rm -f $@; \ $(CC) -MM $(C ...
- ZOJ 3735 dp
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3735 好久没做DP题了,一开始没理解题目里的C(M,3)是干什么,原来就是 ...
- 17.3.12--urllib2模块
1---urllib2是非常强大的Python网络资源访问模块,它的功能和urllib模块相似 python标准库中的urllib2模块可以说是urlib模块的一个升级的复杂版,不需要另外下载, 比如 ...
- [PHP防火墙]输入内容存在危险字符,安全起见,已被本站拦截
之前在很多的网站都看到了360webscan的攻击拦截脚本,正好分析并学习一下. 下载地址:http ://webscan.360.cn/protect/down?domain = blog.dybo ...
- ubuntu18.04国内软件源
ubuntu默认的软件源是国外的,下载软件很慢,需要更新为国内的源以提升速度,现在可以通过ubunt software来设置了,不过还是习惯了命令行修改的方式. 更新方法 123 sudo vi /e ...
- Java常用面试题总结
1.多线程实现方式 2.cookie和session区别 3.数据加密 4.接口并发 5.常用的集合类 6.遍历集合方式 7.接口和抽象类 8.#和$区别 9.防止sql注入 10.springMvc ...
- 箭头函数,闭包函数中的this指向
在javscript中,this 是在函数运行时自动生成的一个内部指针,它指向函数的调用者. 箭头函数有些不同,它的this是继承而来, 默认指向在定义它时所处的对象(宿主对象),而不是执行时的对象. ...
- ios uiimagepickercontroller 选择相册或者拍照上传
首先需要实现UIImagePickerControllerDelegate 代理 实现其imagePickerController 方法 这里用于选择图片或的拍照回调 //调用相机拍照 或者 图库选 ...
- 解决在Anaconda中的cv2在pycharm中不可使用的问题
在Anaconda中已经安装好的opencv模块在pycharm中却不能正常使用,后来发现是pycharm使用的python环境中没有opencv的包,解决方法有两种: 方法一 在pycharm的设置 ...