码迷,mamicode.com
首页 > 其他好文 > 详细

Populating Next Right Pointers in Each Node II--LeetCode

时间:2015-04-11 11:53:04      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   算法   

题目:

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

思路:和上面的一题差不多,只不过需要慢慢的查找,更新的时候也是需要慢慢的查找

void order(BinTree* root)
{
     BinTree* temp = root;
     BinTree* next_head = NULL;
     if(temp == NULL)
      return;
     while(temp != NULL)
     {
                cout<<temp->value<<endl;
                temp = temp->next;
     }
     temp = root;
     while(temp != NULL)
     {
       if(temp->left != NULL)
       {
          next_head= temp->left;
          break;
       }  
       if(temp->right !=NULL)
       {
          next_head = temp->right;
          break;
       } 
       temp = temp->next;        
     }
     order(next_head);
}

// pre_head 一直都是上一层的第一个节点 
void helper_third(BinTree* pre_head)
{
    BinTree* pre = pre_head;
    BinTree* cur_first=NULL; 
    BinTree* cur_second=NULL;
    BinTree* next_head=NULL;
    while(pre != NULL && (cur_first == NULL || cur_second == NULL))
    {
      if(pre->left != NULL)
      {
         if(cur_first == NULL)
         {
           cur_first= pre->left;
           next_head = cur_first;             
         } 
         else if(cur_second ==NULL)
           cur_second = pre->left;                  
      } 
      if(pre->right != NULL)
      {
         if(cur_first == NULL)
         {
           cur_first = pre->right;
           next_head = cur_first;
         }
         else if(cur_second == NULL)
           cur_second = pre->right;              
      }
      if(cur_first != NULL && cur_second != NULL)  
       break;
      pre = pre->next;
    }
    
    if(cur_first == NULL && cur_second == NULL) 
      return ;
    while(pre != NULL)
    {
      cur_first->next = cur_second;
      cur_first = cur_second; 
      if(cur_second == pre->left && pre->right != NULL)
      {
         cur_second = pre->right;
         continue;
      }
      pre = pre->next;
      while(pre != NULL)
      {
        if(pre->left != NULL)
        {
           cur_second = pre->left;   
           break;         
        }
        if(pre->right != NULL)
        {
           cur_second = pre->right;
           break;             
        } 
        pre = pre->next;      
      }    
    }
    if(cur_first != NULL)
     cur_first->next = NULL;
    helper_third(next_head);
}

void Connect_third(BinTree* root)
{
   if(root==NULL)
     return ;
   root->next = NULL;
   helper_third(root);     
}



Populating Next Right Pointers in Each Node II--LeetCode

标签:c++   leetcode   算法   

原文地址:http://blog.csdn.net/yusiguyuan/article/details/44993133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!