You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
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的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  2. [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 ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. LeetCode OJ 116. Populating Next Right Pointers in Each Node

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. [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 ...

  6. 116. Populating Next Right Pointers in Each Node

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  7. leetcode 116 Populating Next Right Pointers in Each Node ----- java

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

随机推荐

  1. Thread--使用condition实现顺序执行

    package condition; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Re ...

  2. opencv进行视频播放每帧处理,读取视频失败

    cv::VideoCapture capture(filename); if (!capture.isOpened()) { cout << "open video error& ...

  3. Docker 三剑客--Machine

    Machine 我们知道在多个集群服务环境下,安装管理Docker的容器,要使用的是Docker Swarm,而使用Docker Swarm的情况是在多个集群的服务器已经搭建好Docker环境的情况下 ...

  4. python导入自定义的库

    一.导入项目文件夹下的模块 1.导入整个模块 import 模块名 2.导入模块的某个函数 from 模块名 import 函数名 示例 untitled是项目文件夹,文件结构如下 ①在a.py导入c ...

  5. ZJNU 1217 - 航线问题——高级

    将所有航线的其中一边排序后,另一边进行类dp 定义一个数组c,c[i]表示在所有能够开通i条航线的组合中,位置序号最大的那条航线的序号的最小值 比如下面一个样例 1 3 2 4 3 1 4 2 此时对 ...

  6. 题解-------[ZJOI2009]对称的正方形

    传送门 题目大意 找到所有的上下左右都相同的正方形. 思路:二分+二维Hash 这道题我们首先想到不能暴力判断一个正方形是否合法. 然后我们发现当一个正方形合法时,以这个正方形为中心且比它小的正方形也 ...

  7. python文件读写 文件修改

    #设置一个变量f为文件对象,并打开文件#写文件#f = open('user.txt','w',encoding='utf-8') #f是一个文件对象f=open(r'c:\Users\PL\Desk ...

  8. LeetCode No.154,155,156

    No.154 FindMin 寻找旋转排序数组中的最小值 II 题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7 ...

  9. windows下pip的安装

    安装地址:https://pypi.python.org/pypi/pip#downloads 下载完成后,找到文件并进行解压,找到下面路径. 打开cmd,cd到当前目录下,然后执行下面命令: pyt ...

  10. 三十七、www服务nginx进阶

    六.查看nginx默认首页和目录:如下,可以看到,默认的目录是html,首页是index.html [root@djw1 conf]# grep html nginx.conf            ...