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. B树及2-3树的python实现

    B树(或称B-树)是一种适用于外查找的树,它是一种平衡的多叉树. 阶为M的B树具有下列结构特征: 1.树的根或者是一片树叶,或者其儿子数在2和M之间. 2.除根节点外的所有非树叶节点儿子数在┌M/2┐ ...

  2. 3224: Tyvj 1728 普通平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...

  3. JLOI 2013 卡牌游戏

    问题描述: N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先 ...

  4. KVC vs KVO(内容为转载记录,整合大家的总结为我所用)

    KVC即key-value coding的缩写, KVO即key-value observing的缩写 假如需要掌握Key-Value Observing机制,那么需要阅读本文应该有帮助.本文提供了K ...

  5. leetcode-173:Binary Search Tree Iterator(Java)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  6. Chapter 17. Objects and Inheritance(对象与继承)

    javascript面向对象编程有几个层面: 1: 单一对象 (covered in Layer 1: Single Objects) 2: 对象之间的 prototype  (described i ...

  7. eclipse, Log4j配置(真心的详细~)

    转自: http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html a). 新建Java Project>>新建pa ...

  8. nginx -t "nginx: [warn] only the last index in "index" directive should be absolute in 6 "的问题解决

    修改完nginx的配置文件之后,执行nginx -t命令提示"nginx: [warn] only the last index in "index" directive ...

  9. HDOJ多校联合第六场

    先一道一道题慢慢补上, 1009.题意,一棵N(N<=50000)个节点的树,每个节点上有一个字母值,给定一个串S0(|S0| <=30),q个询问,(q<=50000),每次询问经 ...

  10. POJ - 2711 Leapin' Lizards

    题目大意: 在一个网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一 ...