Populating Next Right Pointers in Each Node--LeetCode

本文介绍了一种在完美二叉树中填充Next指针的方法,使得每个节点指向其右侧相邻节点。通过层次遍历及递归两种方式实现,并保持常数级别的额外空间消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Given a binary tree

    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

         1
       /  \
      2    3
     / \  / \
    4  5  6  7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \  / \
    4->5->6->7 -> NULL
思路:只是把每个节点都添加了一个next节点,对于一棵树,把next节点都进行赋值,而且是一个完全二叉树,可以使用层次遍历,为每一层都进行改造。

void Connect(BinTree* root)
{
     if(root == NULL)
         return ;
     deque<BinTree*> de;
     de.push_back(root);
     int i,level=1;
     BinTree* cur,*pre; 
     while(!de.empty())
     {
         cur = NULL;
         pre = NULL;
         for(i=0;i<level&&!de.empty();i++)
         {
             if(pre == NULL)
             {
               pre = de.front();
               if(pre->left != NULL)
                de.push_back(pre->left);
               if(pre->right !=NULL)
                de.push_back(pre->right);
               de.pop_front();
               cout<<pre->value<<endl;
               continue;    
             }      
             if(cur == NULL)
             {
               cur = de.front();
               if(cur->left != NULL)
                de.push_back(cur->left);
               if(cur->right !=NULL)
                de.push_back(cur->right);
               de.pop_front();
               cout<<cur->value<<endl;
               continue; 
             }
             pre->next =cur;
             pre = cur;
             cur = de.front();
             de.pop_front();
             if(cur->left != NULL)
               de.push_back(cur->left);
             if(cur->right != NULL)
               de.push_back(cur->right); 
              cout<<cur->value<<endl;       
         }
         
         if(cur == NULL)
           pre->next == NULL;
         else
         {
           pre->next = cur;
           cur->next == NULL;  
         }
         level *=2;            
     }
}

题目的要求是不用额外的空间,加入有一层已经弄好了,那么根据这一层是否可以初始化下一层?可以,从上一层头节点的左边开始即可。最后是改造后的层次遍历

  
void helper(BinTree* pre_head)
{
     if(pre_head->left == NULL )//|| pre_head->left== NULL)
       return ;
     BinTree* cur_first = pre_head->left;
     BinTree* pre = pre_head;
     BinTree* cur_second = pre_head->right;
     while(pre != NULL&&pre->left != NULL)
     {
        cur_first->next = cur_second;
        cur_first = cur_second;
        pre = pre->next;
        if(pre != NULL && pre->left != NULL)
        {
           cur_second = pre->left;
           cur_first->next = cur_second;
           cur_first = cur_second;
           cur_second = pre->right;
        }          
     }
     if(cur_first != NULL)
       cur_first->next =NULL;
     helper(pre_head->left);
}

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

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

ps:这是一个满二叉树,所以对于每一层节点的个数都是一定的,假如题目中没有要求使用常量的空间,那么仍然可以使用层次遍历的方法来处理这个问题,对于每一层的头节点和每一层的尾节点都是知道的,既然要求使用常量的空间复杂度,那么就需要使用递归了!

对于一个函数,如果传递了当前层次的第一个节点pre,那么就可以处理下面那个层次,因为是一个满二叉树,pre节点横向是一个单链表,由这个单链表可以遍历下面层次的各个节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值