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 };
  • 相关阅读:
    敏捷开发
    开撕队-软件需求规格说明书
    开撕队前来问候
    Four-operations: 使用node.js实现四则运算程序
    wordcount
    编码的故事转载2018-02-28更新
    MySQL学习笔记2018-02-07更新
    Linux各文件及目录说明2018-03-01更新
    Linux安全运维笔记2018-03-01更新
    jQuery实现商品五星评价
  • 原文地址:https://www.cnblogs.com/vincently/p/4231931.html
Copyright © 2011-2022 走看看