前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tree - 116. Populating Next Right Pointers in Each Node

Tree - 116. Populating Next Right Pointers in Each Node

作者头像
ppxai
修改2020-09-23 17:35:31
2540
修改2020-09-23 17:35:31
举报
文章被收录于专栏:皮皮星球

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 children. The binary tree has the following definition:

代码语言:javascript
复制
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:

代码语言:javascript
复制
**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.

**思路:**
> 给一棵满二叉树,让把next指针指向同一层,很简单,可以先序遍历做,也可以只用两个指针,核心代码就是对于一个节点,左节点指向右节点,右节点指向自己的下一个的左节点。因为题目要求`only use constant extra space`,所以不能用递归,递归其实也是用了额外的空间。

**代码:**

java:
/*
// Definition for a Node.
 class Node {
 public int val;
 public Node left;
 public Node right;
 public Node next;
代码语言:javascript
复制
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) {
 recursion(root);
 return root;
 }
代码语言:javascript
复制
private void recursion(Node root) {
    if (root == null) return;
    
    if (root.left != null) root.left.next = root.right;
    if (root.right != null && root.next != null) root.right.next = root.next.left;
    
    recursion(root.left);
    recursion(root.right);
}*/

public Node connect(Node root) {
    Node level_start = root;
    
    while (level_start != null) {
        Node cur = level_start;
        while (cur != null) {
            if(cur.left != null) cur.left.next = cur.right;
            if(cur.right != null && cur.next != null) cur.right.next = cur.next.left;
            
            cur = cur.next;
        }
        level_start = level_start.left;
    }
    
    return root;
 }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年08月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档