zoukankan      html  css  js  c++  java
  • LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode: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

    LeetCode:Populating Next Right Pointers in Each Node II                                                                                                                          本文地址

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

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    • You may only use constant extra space.

    For 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

    分析:直接考虑普通的二叉树:层序遍历二叉树,把每一层中前一个节点的next指向后一个节点,使用队列辅助层序遍历时,在队列中用NULL来分割每层的节点,可以通过两题测试的代码如下:

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * struct TreeLinkNode {
     4  *  int val;
     5  *  TreeLinkNode *left, *right, *next;
     6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     void connect(TreeLinkNode *root) {
    12         // IMPORTANT: Please reset any member data you declared, as
    13         // the same Solution instance will be reused for each test case.
    14         if(root == NULL)return;
    15         queue<TreeLinkNode*> myqueue;
    16         myqueue.push(root);
    17         myqueue.push(NULL);//NULL作为队列中每层节点之间的间隔
    18         TreeLinkNode *pre = NULL;
    19         while(myqueue.empty() == false)
    20         {
    21             TreeLinkNode *p = myqueue.front();
    22             myqueue.pop();
    23             if(p != NULL)
    24             {
    25                 if(p->left)myqueue.push(p->left);
    26                 if(p->right)myqueue.push(p->right);
    27             }
    28             else if(myqueue.empty() == false)
    29                 myqueue.push(NULL);
    30             if(pre != NULL)pre->next = p;
    31             pre = p;
    32         }
    33     }
    34 };

    【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3437497.html

  • 相关阅读:
    调试windows service的OnStart事件.
    Windows 7 下 ORACLE10G安装方法
    汉字编码对照表(gb2312/Big5/GB2312)
    FastReport For Delphi7 通用安装方法
    Visual Studio 2008 不能更改安装目录的原因
    全国信息技术标准化技术委员会汉字内码扩展规范(GBK)
    字符编码详解及由来(UNICODE,UTF8,GBK)[转帖]
    淘宝收藏
    蛙蛙推荐:设计一个Tracing组件
    收藏:SYN 攻击原理以及防范技术
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3437497.html
Copyright © 2011-2022 走看看