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

    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

    借鉴上一篇的方法,先找到下一行的第一个节点,然后把一整行处理完毕。
    违规了,用到了额外的空间,但是很好理解。
    这里用到的额外的空间vector<TreeLinkNode*>nodes 保存下一行找到的所有节点。
    第一个节点就是下次行遍历的起点。
     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         if(!root) return;
    13         vector<TreeLinkNode*> nodes;
    14         while(root)
    15         {
    16             if(root->left) nodes.push_back(root->left);
    17             if(root->right) nodes.push_back(root->right);
    18             root = root->next;
    19         }
    20         if(nodes.size()==0)
    21         {
    22             root = NULL;
    23         }
    24         else
    25         {
    26             root = nodes[0];
    27             if(nodes.size()>1)
    28             {
    29                 for(int i=1;i<nodes.size();i++)
    30                 {
    31                     nodes[i-1]->next = nodes[i];
    32                 }
    33             }
    34         }
    35         connect(root);
    36     }
    37 };
    那么,我们可不可以不违规,只用O(1)的空间呢?
    答案是肯定的。
    我们知道记住扫描下一行时,第一个出现的就可以了。
    是不是很简单呢。。。和上面的原理一模一样,理解起来麻烦一点而已,不过,上面的你理解了,这个理解起来还是很简单的。
     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         if(!root) return;
    13         TreeLinkNode* first_node ;
    14         TreeLinkNode* pre_node ;
    15         TreeLinkNode* next_node ;
    16         int i=0;
    17         while(root)
    18         {
    19             if(root->left)
    20             {
    21                 i++;
    22                 if(i==1)
    23                 {
    24                     first_node = root->left;
    25                     pre_node = root->left;
    26                 }
    27                 else
    28                 {
    29                     next_node = root->left;
    30                     pre_node->next = next_node;
    31                     pre_node = next_node;
    32                 }
    33             }
    34             if(root->right)
    35             {
    36                 i++;
    37                 if(i==1)
    38                 {
    39                     first_node = root->right;
    40                     pre_node = root->right;
    41                 }
    42                 else
    43                 {
    44                     next_node = root->right;
    45                     pre_node->next = next_node;
    46                     pre_node = next_node;
    47                 }
    48             }
    49             root = root->next;
    50         }
    51         if(i==0) connect(NULL);
    52         else connect(first_node);
    53     }
    54 };


  • 相关阅读:
    tmux 的基本使用
    ffmpeg(1) 基础框架
    VUE页面跳转方式
    nextcloud 中文乱码解决方案
    mysql8 navicat远程链接失败
    prometheus+grafana实现服务监控
    sqlalchemy ————关联表
    Python flask自定义异常信息,返回json格式的异常
    sqlalchemy 查询结果转json个人解决方案
    Linux添加字体
  • 原文地址:https://www.cnblogs.com/Sean-le/p/4783228.html
Copyright © 2011-2022 走看看