5.4.7—二叉树的递归—Populating Next Right Pointers in Ea Node

描述
Given a binary tree
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
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
Aer calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL



#ifndef BINTREE_H_
#define BINTREE_H_
#include<vector>
#include<iostream>
struct BinTree
{
	int value;
	BinTree *left;
	BinTree *right;
	BinTree *sbling;
};
BinTree *CreateNode(int data);
void Connect(BinTree *pRoot, BinTree *pLeft, BinTree *pRight);
void PrintOneNode(BinTree *pRoot);
void PrintTree(BinTree *pRoot);
void DestroyTree(BinTree *pRoot);

#endif

#include"BinTree.h"
using namespace std;
BinTree *CreateNode(int data)
{
	BinTree *pRoot = new BinTree();
	pRoot->value = data;
	pRoot->left = pRoot->right =pRoot->sbling= NULL;
	return pRoot;
}
void Connect(BinTree *pRoot, BinTree *pLeft, BinTree *pRight)
{
	pRoot->left = pLeft;
	pRoot->right = pRight;
}
void PrintOneNode(BinTree *pRoot)
{
	if (pRoot)
	{
		cout << pRoot->value << endl;
		if (pRoot->left)
			cout << pRoot->value << " :left node is:" << pRoot->left->value << endl;
		else 
			cout << pRoot->value << " :left node is null" << endl;
		
		if (pRoot->right)
			cout << pRoot->value << " :right node is:" << pRoot->right->value << endl;
		else
			cout << pRoot->value << " :right node is null" << endl;
		
		
		if (pRoot->sbling)
			cout << pRoot->value << ":sbling node is:" << pRoot->sbling->value << endl;
		else 
			cout << pRoot->value << " :sbling node is null" << endl;
	}
	else
	{
		cout << "the node is NULL!" << endl;
	}
}
void PrintTree(BinTree *pRoot)
{
	PrintOneNode(pRoot);
	if (pRoot->left)
		PrintTree(pRoot->left);
	if (pRoot->right)
		PrintTree(pRoot->right);
}
void DestroyTree(BinTree *pRoot)
{
	if (!pRoot)
		return;
	else
	{
		BinTree *pLeft = pRoot->left;
		BinTree *pRight = pRoot->right;
		delete pRoot;
		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}

#include"BinTree.h"
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//====递归
void Populate1(BinTree *pRoot)
{
	if (!pRoot||pRoot->left==NULL)
		return;
	BinTree *p = pRoot;
	while (p)
	{
		p->left->sbling = p->right;
		if (p->sbling)
			p->right->sbling = p->sbling->left;
		p = p->sbling;
	}
	Populate1(pRoot->left);
}
//===迭代
void Populate2(BinTree *pRoot)
{
	if (!pRoot || pRoot->left == NULL)
		return;
	BinTree *p = NULL;
	BinTree *q = pRoot;
	while (q->left)
	{
		p = q;
		while (p)
		{
			p->left->sbling = p->right;
			if (p->sbling)
				p->right->sbling = p->sbling->left;
			p = p->sbling;
		}
		q = q->left;
	}
}
int main()
{
	BinTree *p6 = CreateNode(6);
	BinTree *p4 = CreateNode(4);
	BinTree *p1 = CreateNode(1);
	BinTree *p7 = CreateNode(7);
	BinTree *p8 = CreateNode(8);
	BinTree *p5 = CreateNode(5); 
	BinTree *p10 = CreateNode(10);
	Connect(p6, p4, p8);
	Connect(p4, p1, p7);
	Connect(p8, p5, p10);

	//Populate1(p6);
	Populate2(p6);

	PrintTree(p6);
	DestroyTree(p6);
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值