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 };
  • 相关阅读:
    c# 事件阻断
    正则语义化API
    c# 防止继承和单例
    Maxscript 控制流混淆
    3dmax快速安装补丁的方法
    c# 使用类中的方法更新自己
    Maxscript 变量作用域
    Maxscript 键值对
    Maxscript 数据结构和算法记录
    Datawhale 之NLP学习-打卡(五)
  • 原文地址:https://www.cnblogs.com/vincently/p/4231931.html
Copyright © 2011-2022 走看看