zoukankan      html  css  js  c++  java
  • 【leetcode】Populating Next Right Pointers in Each Node II

    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
    
     
    与I的思路类似
    left->next=father->right
    right->next=father->next->left;
     
    只是当我们没有找到时,father=father->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        
    13         if(root==NULL)
    14         {
    15             return;
    16         }
    17        
    18         if(root->left!=NULL)
    19         {
    20             if(root->right!=NULL)
    21             {
    22                 root->left->next=root->right;
    23             }
    24             else
    25             {
    26                 TreeLinkNode *tmp=root->next;
    27                 root->left->next=NULL;
    28                 while(tmp!=NULL)
    29                 {
    30                     if(tmp->left!=NULL)
    31                     {
    32                         root->left->next=tmp->left;
    33                         break;
    34                     }
    35                    
    36                     if(tmp->right!=NULL)
    37                     {
    38                         root->left->next=tmp->right;
    39                         break;
    40                     }
    41                     tmp=tmp->next;
    42                 }
    43             }
    44         }
    45        
    46         if(root->right!=NULL)
    47         {
    48             TreeLinkNode *tmp=root->next;
    49             root->right->next=NULL;
    50             while(tmp!=NULL)
    51             {
    52                 if(tmp->left!=NULL)
    53                 {
    54                     root->right->next=tmp->left;
    55                     break;
    56                 }
    57                    
    58                 if(tmp->right!=NULL)
    59                 {
    60                     root->right->next=tmp->right;
    61                     break;
    62                 }
    63                 tmp=tmp->next;
    64             }
    65         }
    66        
    67        
    68         connect(root->right);
    69         connect(root->left);
    70        
    71     }
    72 };
  • 相关阅读:
    POJ 1182 食物链 并查集
    POJ 3666 Making the Grade DP
    POJ 1631 Bridging signals DP(最长上升子序列)
    POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
    利用UUID 随机生成8位短号
    JS遍历子孙树
    前端中this的用法
    利用fetch进行POST传参
    SQL UNION 操作符
    Python 抖音机器人,论如何在抖音上找到漂亮小姐姐?
  • 原文地址:https://www.cnblogs.com/reachteam/p/4202226.html
Copyright © 2011-2022 走看看