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. cx_Oracle ORA-24315: 非法的属性类型的解决办法

    网上查找原因说: 出现该错误的原因是因为版本不兼容. 检查了一下python版本和oracle 的版本,我的oracle client是10g的,python的版本是2.7, 但我安装的cx_orac ...

  2. 转最简便安装python+selenium-webdriver环境方法

    最简便安装python+selenium-webdriver环境方法 from:http://www.easonhan.info/python/2013/12/07/active-python-ins ...

  3. wpf 只在window是ShowDialog打开时才设置DialogResult

    //only set DialogResult when window is ShowDialog before if(System.Windows.Interop.ComponentDispatch ...

  4. WinForm小小应用

    制作日历计划任务 private void BeginTask() { Thread th = new Thread(//建立线程 (() =>//使用Lambda表达式 { while (tr ...

  5. vs2013运行c语言出现:无法查找或打开 PDB 文件。

    vs2013运行c语言出现:无法查找或打开 PDB 文件.    “ConsoleApplication1.exe”(Win32): 已加载“C:\Users\hp\Documents\Visual ...

  6. Oracle数据库安装完成之后的启动操作

    由于是菜鸟,在 完成Oracle数据库的安装之后,不知道该怎么启动.在经过一番折腾之后明白了其中的一些道理,总结如下: 其实Oracle数据库和Mysql数据库的启动都是相同的原理. Mysql数据库 ...

  7. BT5之配置笔记

    BT5本来就是用Ubuntu 10.04做得蓝本,所以,我在配置BT5的时候,基本上都是按照Ubuntu 10.04的配置方法,在配置BT5 1    系统基本设置 1.1  安装Ubuntu10.0 ...

  8. 网页中插入Flvplayer视频播放器代码

    http://blog.csdn.net/china_skag/article/details/7424019 原地址:http://yuweiqiang.blog.163.com/blog/stat ...

  9. UVA 558 Wormholes

    要问是否存在一个总权重为负数的环,用dfs即可解决. time:33ms #include <cstdio> #include <cstring> #define N 3000 ...

  10. 使用php实现权限管理模块

    在说权限管理模块前,应该先知道权限管理模块要有哪些功能: 1.用户只能访问,指定的控制器,指定的方法 2.用户可以存在于多个用户组里 3.用户组可以选择,指定的控制器,指定的方法   4.后台可以添加 ...