1.题目描述

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

2.解法分析

这道题目给了一个很强的约束,那就是可以假设树结果为满二叉树,满二叉树每一层的节点个数是确定的,如果将树中的元素按照层序遍历的方式编号,那么编号为n的节点在哪一层也是轻易可知的(假设编号从1开始,那么节点n所在层为log2n+1),同样,每层最后一个节点的编号也是已知的(m层的最后一个元素编号为2m-1),基于这个强约束,我决定用层序遍历的方式解答这个题目。

/**

 * 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) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if(root == NULL)return;

        

        queue<TreeLinkNode *> q;

        int count=0;

        int depth =1;

        TreeLinkNode * cur=NULL;

        q.push(root);

        while(!q.empty())

        {

            cur=q.front();

            q.pop();

            count++;

            if(count == pow(2,depth)-1)

            {

                cur->next = NULL;

                depth++;

            }

            

            else

            {

                cur->next = q.front();

            }

            

            if(cur->left!=NULL)q.push(cur->left);

            if(cur->right!=NULL)q.push(cur->right);

        }

        

        

    }

};

代码一次通过,真爽!

leetcode—Populating Next Right Pointers in Each Node的更多相关文章

  1. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

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

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

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

  4. LeetCode——Populating Next Right Pointers in Each Node II

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

  5. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  6. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  7. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  8. LeetCode - Populating Next Right Pointers in Each Node II

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

  9. LeetCode: Populating Next Right Pointers in Each Node 解题报告

    Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode {      Tree ...

  10. [LeetCode] [LeetCode] 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. IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目

    以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...

  2. 渲染统计窗口(Rendering Statistics Window)

    FPS(Timer per frame and FPS) frames per seconds表示引擎处理和渲染一个游戏帧所花费的时间,该数字主要受到场景中渲染物体数量和GPU性能的影响,FPS数值越 ...

  3. appfabric 简单应用

    http://msdn.microsoft.com/en-us/library/ee790941(v=azure.10).aspx Preparing the Visual Studio Projec ...

  4. 视图--bai

    /*视图的必要性 create view population_all_view as select xxxx 详细信息 from qgck where rownum<500 -- sql语句不 ...

  5. 解决win8找不到没有AppData文件夹

    现象:今天打开win8,由于是从xp直接过渡到win8,所以想寻找类似于AppData的文件夹.但是在user/用户名/下面木有. 解决:这是个隐藏文件,和xp一样,取消隐藏文件夹即可看到. 然后就可 ...

  6. Bash Promot

    http://www.askapache.com/linux/bash-power-prompt.html 强大的Bash Promt命令 http://bbs.51cto.com/thread-11 ...

  7. linux下安装jira详细步骤

    首先从官网下载jdk的安装包,将jdk的安装包上传到虚拟机或者服务器,在./usr/local/目录下面创建一个java目录:mkdir java 等等,具体祥看本文,希望对你有所帮助 linux下安 ...

  8. Ubuntu10.10 安装scim

    Ubuntu10.10 上没有找到默认的输入法,所以要安装一个中文输入法,网上好多介绍的,但都 不怎么好用,下面参考http://blog.csdn.net/caodesheng110/article ...

  9. OpenSSL 与 SSL 数字证书概念贴

    SSL/TLS 介绍见文章 SSL/TLS原理详解(http://seanlook.com/2015/01/07/tls-ssl). 如果你想快速自建CA然后签发数字证书,请移步 基于OpenSSL自 ...

  10. 同一客户代码下一个CPN对应多个FG会出现CPN描述一样的问题

    Q&A : D2E01客户下已经存在CPN: TP1062/A 对应FG: P5M2812512AFHHF0 直接在客户代码下无法新建,只能在交叉信息新建CPN: TP1062/A, 但无法修 ...