zoukankan      html  css  js  c++  java
  • [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

    跟上一题一样样的。

     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 == NULL) {
    13             return;
    14         }
    15         queue<TreeLinkNode* > q;
    16         TreeLinkNode *p;
    17         int idx = 1, n;
    18         q.push(root);
    19         while (!q.empty()) {
    20             n = idx - 1;
    21             idx = 0;
    22             p = q.front();
    23            if (q.front()->left != NULL) {
    24                     q.push(q.front()->left);
    25                     idx++;
    26                 }
    27                 if (q.front()->right != NULL) {
    28                     q.push(q.front()->right);
    29                     idx++;
    30                 }
    31             q.pop();
    32             for (int i = 0; i < n; ++i) {
    33                 p->next = q.front();
    34                 if (q.front()->left != NULL) {
    35                     q.push(q.front()->left);
    36                     idx++;
    37                 }
    38                 if (q.front()->right != NULL) {
    39                     q.push(q.front()->right);
    40                     idx++;
    41                 }
    42                 p = p->next;
    43                 q.pop();
    44             }
    45             p->next = NULL;
    46         } 
    47     }
    48 };

    但是,如果只要常数的空间复杂度的话,我们可以利用next指针,因为在访问当前层时,当前行的next指针已经在访问上一层时连接好了。

     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         TreeLinkNode *prev, *next;
    13         while (root) {
    14             prev = nullptr; next = nullptr;
    15             for (; root != nullptr; root = root->next) {
    16                 if (next == nullptr) next = root->left ? root->left : root->right;
    17                 if (root->left) {
    18                     if (prev) prev->next = root->left;
    19                     prev = root->left;
    20                 }
    21                 if (root->right) {
    22                     if (prev) prev->next = root->right;
    23                     prev = root->right;
    24                 }
    25             }
    26             root = next;
    27         }
    28     }
    29 };
  • 相关阅读:
    搜索
    c++ map与unordered_map区别及使用
    01BFS
    宇智波程序笔记55-Flutter 混合开发】嵌入原生View-iOS
    宇智波程序笔记54-解Bug之路-记一次线上请求偶尔变慢的排查
    宇智波程序笔记53-从红黑树的本质出发,彻底理解红黑树!
    宇智波程序笔记52-最受欢迎的微服务框架概览
    宇智波程序笔记51-JDK 15安装及新特性介绍
    宇智波程序笔记50-解Bug之路-记一次线上请求偶尔变慢的排查
    宇智波程序笔记49-link JDBC Connector:Flink 与数据库集成最佳实践
  • 原文地址:https://www.cnblogs.com/easonliu/p/3639531.html
Copyright © 2011-2022 走看看