zoukankan      html  css  js  c++  java
  • [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 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     TreeLinkNode *NextNode(TreeLinkNode *p) {
    12         while (p) {
    13             if (p->left) 
    14                 return p->left;
    15             if (p->right)
    16                 return p->right;
    17             p = p->next;
    18         }
    19         
    20         return NULL;
    21     }
    22     
    23     void connect(TreeLinkNode *root) {
    24         if (root == NULL) return;
    25         TreeLinkNode *level_begin = root;
    26         while (level_begin) {
    27             TreeLinkNode *cur = level_begin;
    28             while (cur) {
    29                 if (cur->left)
    30                     cur->left->next = (cur->right != NULL) ? cur->right : NextNode(cur->next);
    31                 if (cur->right)
    32                     cur->right->next = NextNode(cur->next);
    33                 cur = cur->next;
    34             }
    35             level_begin = NextNode(level_begin); //下一层的开始节点
    36         }
    37     }
    38 };

    思路二:

      一个prev记录当前层前一节点是啥(用来连接的
      一个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         while(root) {
    13             TreeLinkNode *next = NULL; //the first node of next level
    14             TreeLinkNode *prev = NULL; //previous node on the same level
    15             for (; root; root = root->next) {
    16                 if (!next) next = root->left ? root->left : root->right;
    17                 
    18                 if (root->left) {
    19                     if (prev) prev->next = root->left;
    20                     prev = root->left;
    21                 }
    22                 if (root->right) {
    23                     if (prev) prev->next = root->right;
    24                     prev = root->right;
    25                 }
    26             }
    27             
    28             root = next;
    29         }
    30     }
    31 };
  • 相关阅读:
    Hasura GraphQL schema 生成是如何工作的
    一张方便的graphql schema 语言手册
    使用lua graphql 模块让openresty 支持graphql api
    PostgREST docker-compose 试用
    subzero 基于postgrest && openresty && rabbitmq 的快速rest/graphql 开发平台
    使用blessed 开发丰富的cli 应用
    一个方便查看数据库转换rest/graphql api 的开源软件的github 项目
    treeql 基于rest 标准的接口开发协议
    graphql-modules 企业级别的graphql server 工具
    hangfire docker-compose 运行
  • 原文地址:https://www.cnblogs.com/vincently/p/4231931.html
Copyright © 2011-2022 走看看