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:
  1. struct Node {
  2. int val;
  3. Node *left;
  4. Node *right;
  5. Node *next;
  6. }

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:

  1. 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}
  2.  
  3. 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}
  4.  
  5. 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.
  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. public int val;
  5. public Node left;
  6. public Node right;
  7. public Node next;
  8.  
  9. public Node() {}
  10.  
  11. public Node(int _val,Node _left,Node _right,Node _next) {
  12. val = _val;
  13. left = _left;
  14. right = _right;
  15. next = _next;
  16. }
  17. };
  18. */
  19. class Solution {
  20. public Node connect(Node root) {
  21. if (root == null) {
  22. return null;
  23. }
  24.  
  25. if (root.left != null) {
  26. root.left.next = root.right;
  27. if (root.next != null) {
  28. root.right.next = root.next.left;
  29. }
  30. }
  31. connect(root.left);
  32. connect(root.right);
  33. return root;
  34. }
  35. }

[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. 跟踪LinkedList源码,通过分析双向链表实现原理,自定义一个双向链表

    1.LinkedList实现的基本原理 LinkedList是一个双向链表,它主要有两个表示头尾节点的成员变量first  .last,因其有头尾两个节点,所以从头或从尾操作数据都非常容易快捷.Lin ...

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

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

  3. Docker部署freeswitch

    1. clone配置文件到本地服务器 git clone https://github.com/BetterVoice/freeswitch-container.git 相关Dockerfile如下: ...

  4. c# 之循环 ,while 和do---while还有for

    ㈠while循环 循环条件 是个bool值,为true时执行循环,为false退出循环.break一般不单独的使用,而是跟着if判断一起使用,表示,当满足某些条件的时候,就退出循环了. 循环体 一般总 ...

  5. PAT Advanced 1089 Insert or Merge (25) [two pointers]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  6. webgis笔记

    3.8(02) .特点:由服务端进行数据管理 开源的GO sever WMS/WCS/WTS 1sever/2engine/3database/4standard 扩展的空间数据库,存矢量.栅格.直接 ...

  7. day63-html-列表,表格,标签的嵌套规则

    1.列表 1.无序列表 <ul type="disc"> <li>a</li> <li>b</li> </ul&g ...

  8. 以KNN为例用sklearn进行数据分析和预测

    准备 相关的库 相关的库包括: numpy pandas sklearn 带入代码如下: import pandas as pd import numpy as np from sklearn.nei ...

  9. Centos7.6环境中安装zabbix3.4

    官网链接:https://www.zabbix.com/documentation/3.4/zh/manual/installation/install_from_packages 部署环境 虚拟机服 ...

  10. Linux] Git: push 出错的解决 master -> master (branch is currently checked out)

      在使用Git Push代码到数据仓库时,提示如下错误: [remote rejected] master -> master (branch is currently checked out ...