【leetcode】Populating Next Right Pointers in Each Node

本文介绍如何在二叉树中填充每个节点的Next指针,使其指向右侧相邻节点。提供两种情况下的解决方案:完美二叉树及一般二叉树,并给出相应的代码实现。

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

Populating Next Right Pointers in Each Node


链接:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/


描述:

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
解法:
本题比较简单,注意 判断NULL的情况。

代码如下:

    void connect(TreeLinkNode *root) {
    	if( root == NULL) return ;
    	TreeLinkNode *preP = root;
    	TreeLinkNode *curP = root->left;
    	root->next = NULL;
    
    	while( curP )
    	{
    		TreeLinkNode *tempP = preP;
    		while( curP )
    		{
    			if( preP == NULL){
    				curP->next = NULL;
    				curP = NULL;
    			}else if( curP == preP->left){
    				curP->next = preP->right;
    				curP = curP->next;
    				preP = preP->next;
    			}else{
    				curP->next = preP->left;
    				curP = curP->next;
    			}
    		}
    		preP = tempP->left;
    		curP = preP->left;
    	}
    }


Populating Next Right Pointers in Each Node II
链接:https://oj.leetcode.com/problems/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

解法:
本题比上一题,复杂,非递归解法如下:

    void connect(TreeLinkNode *root) {
    	if(root == NULL) return ;
    	root->next = NULL;
    	TreeLinkNode *prep = root;
    	TreeLinkNode *curp;
    	if(root->left)
    		curp = root->left;
    	else
    		curp = root->right;
    
    	while(curp)
    	{
    		TreeLinkNode *tempcurp=curp;
    		while(curp)
    		{
    			if(prep == NULL)
    			{
    				curp->next = NULL;
    				break;
    			}else if( curp == prep->right)
    			{
    				prep = prep->next;
    			}else if( curp == prep->left)
    			{
    				if( prep->right )
    				{
    					curp->next = prep->right;
    					curp = curp->next;
    				}
    				prep = prep->next;
    			}else{
    				while(prep && prep->left == NULL && prep->right == NULL)
    					prep = prep->next;
    				if(prep){
    					if(prep->left)
    						curp->next  = prep->left;
    					else
    						curp->next = prep->right;
    					curp = curp->next;
    				}
    			}
    		}
    		prep = tempcurp;
    		while(prep && prep->left == NULL && prep->right == NULL)
    			prep = prep->next;
    		if(prep && prep->left)
    			curp = prep->left;
    		else if(prep && prep->right)
    			curp = prep->right;
    		else
    			break;
    	}
    }

递归解法:

    void connect(TreeLinkNode *root) {
        if(root == NULL) return;
        TreeLinkNode dumy(-1);
        //dumy.next = root;
        
        for(TreeLinkNode *cur = root, *pre = &dumy; cur ; cur = cur->next){
            if(cur->left != NULL){
                pre->next = cur->left;
                pre = pre->next;
            }
            if(cur->right != NULL){
                pre->next = cur->right;
                pre = pre->next;
            }
        }
        connect(dumy.next);
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值