一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

Given a binary tree

struct TreeLinkNode {

TreeLinkNode *left;

TreeLinkNode *right;

TreeLinkNode *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.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,

Given the following perfect binary tree,

    1
  /  \
 2    3
/ \  / \
4  5  6  7

After calling your function, the tree should look like:

    1 -> NULL
  /  \
 2 -> 3 -> NULL
/ \  / \
4->5->6->7 -> NULL

(二)解题

题目大意:定义一个新的二叉树结构,增加一个新指针next指向该节点右侧相邻的节点。

解题思路:考虑到要指向右侧相邻的节点,可以采用层序遍历的方式来求解

将每一层的节点用next连接起来即可!

层序遍历可以参考:【一天一道LeetCode】#102. Binary Tree Level Order Traversal

废话不说,看AC代码:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(root==NULL) return;
        queue<TreeLinkNode *> myque;
        myque.push(root);
        while(!myque.empty())
        {
            queue<TreeLinkNode *> temp_que;
            TreeLinkNode *pre = NULL;
            while(!myque.empty())
            {
                TreeLinkNode *temp = myque.front();
                myque.pop();
                if(pre==NULL) pre = temp;//相当于每一层的头节点
                else {
                    pre->next = temp;//用next指针连接每一层的节点
                    pre = temp;
                }
                if(temp->left!=NULL) temp_que.push(temp->left);//下一层
                if(temp->right!=NULL) temp_que.push(temp->right);//下一层
            }
            pre->next=NULL;//最后一个节点的next需要指向NULL
            myque=temp_que;//进入下一层操作
        }
    }
};

【一天一道LeetCode】#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 ----- java

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

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

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

  5. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

  6. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

  7. [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

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

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

  9. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  10. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. Docker学习笔记【一】

    [本篇学习笔记来源于 Docker 从入门到实践] 1.什么事Docker?[What] Docker在容器的基础上,进行了进一步的封装,从文件系统.网络互联到进程隔离等,极大的简化了容器的创建和维护 ...

  2. diango-团队介绍

    1.使用django-admin startproject show创建项目,并使用python manage.py startapp team_show创建应用 2.进行相关的配置 3.代码的实现

  3. Spring的注解@Qualifier小结

    有以下接口: public interface EmployeeService { public EmployeeDto getEmployeeById(Long id); } 有两个实现类: @Se ...

  4. Jenkins的关闭、重启

    以前一直用从cmd进入jenkins的安装目录,执行jenkins stop/start,但是新的jenkins有更加方便功能 关闭jenkins服务 只需要在访问jenkins服务器的网址url地址 ...

  5. Oracle中的4大空值处理函数用法举例

    nvl(exp1,exp2):                           如果exp1为空,则返回exp2:否则返回exp1nvl2(exp1,exp2,exp3):             ...

  6. java中的final和volatile详解

    相比synchronized,final和volatile也是经常使用的关键字,下面聊一聊这两个关键字的使用和实现 1.使用 final使用: 修饰类表示该类为终态类,无法被继承 修饰方法表示该方法无 ...

  7. Python3 JSON 数据解析

    JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. Python3 中可以使用 json 模块来对 JSON 数据进 ...

  8. Android广播的发送与接收

    Android广播的发送与接收 效果图 广播发送 广播分为有序广播和无序广播 有序广播与无序广播的区别 无序广播:只要是广播接收者指定了接收的事件类型,就可以接收到发送出来的广播消息.不能修改消息. ...

  9. 27 自定义View 和案例

    有时安卓提供的View不满足我们的需求时可以创建一个类继承View或其子类重写方法 如 package com.qf.sxy.day28_customview.view; import android ...

  10. #pragma pack(x) CPU对齐

    编译器会尽量把成员对齐以提高内存的命中率.对齐是可以更改的,使用"#pragma pack(x)" 可以改变编译器的对齐方式. C++固有类型的对界取编译器对齐方式与自身大小中较小 ...