Leetcode Populating Next Right Pointers in Each Node

Populating Next Right Pointers in Each Node

Total Accepted:6129 Total Submissions:17922 My Submissions

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 toNULL.

Initially, all next pointers are set toNULL.

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

直接上递归法

void connect(TreeLinkNode *root) 
	{
		if (!root || !root->left) return;
		root->left->next = root->right;
		con(root->left, root->right);
	}
	void con(TreeLinkNode *lTree, TreeLinkNode *rTree)
	{
		if (lTree->left) lTree->left->next = lTree->right;
		if (rTree->left) rTree->left->next = rTree->right;
		else return;
		lTree->right->next = rTree->left;
		con(lTree->left, lTree->right);
		con(lTree->right, rTree->left);
		con(rTree->left, rTree->right);
	}


下面参考leetcode论坛上的程序写了个程序

//新知识点:
//重点注意:利用新构造的数据结构
void connect(TreeLinkNode* root) 
{
	if (!root || !root->left || !root->right)
		return;
	TreeLinkNode* rightSibling;
	TreeLinkNode* p1 = root;
	while (p1) 
	{
		rightSibling = p1->next? p1->next->left:NULL;
		p1->left->next = p1->right;
		p1->right->next = rightSibling;
		p1 = p1->next;
	}
	connect(root->left);
}

有人会觉得使用递归并不符合常数空间的题意,那么可以改成非递归:

void connect(TreeLinkNode *root) 
	{
		TreeLinkNode *nextLev = root? root->left:NULL;
		while (nextLev)
		{
			for ( ; root; root = root->next) 
			{
				TreeLinkNode *rightSibling = root->next? root->next->left:NULL;
				root->left->next = root->right;
				root->right->next = rightSibling;
			}
			root = nextLev;
			nextLev = nextLev->left;
		}
	}

//2014-2-17 update
	void connect(TreeLinkNode *root) 
	{
		if (!root) return;
		TreeLinkNode *next_level = root->left;
		while (next_level)
		{
			root->left->next = root->right;
			TreeLinkNode *pre = root->right;
			root = root->next;
			while (root)
			{
				pre->next = root->left;
				pre = root->left->next = root->right;
				root = root->next;
			}
			root = next_level;
			next_level = root->left;
		}
	}

//2014-2-17 update
	void connect(TreeLinkNode *root) 
	{
		if (!root) return;
		while (root->left)
		{
			TreeLinkNode *next_level = root->left;
			root->left->next = root->right;
			TreeLinkNode *pre=root->right;			
			for (root=root->next; root; root = root->next)
			{
				pre->next = root->left;
				pre = root->left->next = root->right;
			}
			root = next_level;
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值