题目

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.
Recursive approach is fine, implicit stack space does not count as extra space for this problem.
Example:

Given the following binary tree,

     1
   /  \
  2    3
 / \    \
4   5    7

After calling your function, the tree should look like:

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

解答

这题就是层序遍历二叉树。。。又是一遍就AC了。

下面是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:
    queue<TreeLinkNode *> q;
    void connect(TreeLinkNode *root) {
        if(root == NULL){
            return ;
        }
        q.push(root);
        int i;
        while(i = q.size()){
            while(i--){
                TreeLinkNode *temp = q.front();
                q.pop();
                if(i == 0){
                    temp->next = NULL;
                }
                else{
                    temp->next = q.front();
                }
                if(temp->left != NULL){
                    q.push(temp->left);
                }
                if(temp->right != NULL){
                    q.push(temp->right);
                }
            }
        }
    }
};

137

LeetCode OJ 117. Populating Next Right Pointers in Each Node II的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  5. 【LeetCode OJ】Populating Next Right Pointers in Each Node II

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...

  6. LeetCode OJ: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 ...

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

  8. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  9. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

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

随机推荐

  1. 当你在web项目下新建一个class时package位置如果发生红色波浪错误,提示为”The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files“

    问题是这样的如下图: 问题的原因: 1.配置tomcat7.0的时候自己设置了jre的版本1.8,而没有用myeclipse10自带的jre1.6,导致了出现了差错!

  2. Debian下安装docker

    1.安装docker.io包之前,需要先设置使用backports源 编辑/etc/apt/sources.list文件,加入下面这一句: deb http://http.debian.net/deb ...

  3. CCF-权限查询-201612-3

    这道题,开始只有10分.....原因是将false 写成了 flase 我要吐血而亡....关键是还debug了半天,以为是逻辑错了 不过亮点是代码很简洁,网上140+的代码看着真复杂 核心: 做题之 ...

  4. mysql与mysqli的区别

    博客搬家了,欢迎大家关注,https://bobjin.com mysqli连接是永久连接,而MySQL是非永久连接. mysql连接:每当第二次使用的时候,都会重新打开一个新的进程. mysqli连 ...

  5. Yii2项目高级模版 三个模块在同一个目录下的重定向配置

    最近做项目用到的,非常好用. 修改 advanced/backend/config/main.PHP 文件如下: return [ 'homeUrl' => '/admin', 'compone ...

  6. Java 初始 多态

    什么是多态 简单的来说就是具有多种形态的能力的特征 package ten; public interface Day1 { public void ring(); } package ten; pu ...

  7. C#高低位分解转换备忘

    private void Form1_Load(object sender, EventArgs e) { , , , , , ); var arr = long2LowHight(time.ToFi ...

  8. 在Cygwin中出现JAVA_HOME出现故障找不到出现故障

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012516914/article/details/37689937 JAVA_HOME出现故障后查 ...

  9. [转].NET 性能测试工具 -- 事件跟踪器(ETW)

    .NET 性能测试工具 -- 事件跟踪器(ETW) 内容预告: Windows内置工具(性能计数器) 事件跟踪器(WPT,PerfMoniter,PerfView,自定义ETW) 时间分析 内存分配分 ...

  10. 使用lite-server

    进入项目根目录,执行下列步骤 安装lite-server npm install lite-server 新建配置文件bs-config.json bs-config.json中可以: 指定监听的端口 ...