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 };
  • 相关阅读:
    WPF 本地化语言设置
    WPF 调节树状图滚动条值
    WPF中ListBox的使用注意事项
    SQL 树状结构表中查出所所有父级/子级
    Vue创建
    wpf 控件注意事项
    链表习题(1)-设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点
    排序-快速排序
    排序-堆排序
    图-图的遍历
  • 原文地址:https://www.cnblogs.com/vincently/p/4231931.html
Copyright © 2011-2022 走看看