一篇文章解决所有LeetCode树的问题

本文介绍了一种创建随机二叉树的方法,并演示了前序、中序遍历及分层遍历算法。此外,还实现了二叉树中添加特定行的功能以及两棵二叉树的合并操作。

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

// ConsoleApplication14.cpp : 定义控制台应用程序的入口点。
/*初值指针赋值NULL且
pbtree t;
t=(pbtree)malloc(sizeof(pbtree));*/
//

#include "stdafx.h"
#include"iostream"
#include <queue>
using namespace std;
typedef struct Node
{
	int data;
	Node * leftcd;
	Node * rightcd;
}BTree, *pBtree;
void creat(pBtree &T, int *arr, int begin, int end)
{
	if (begin > end)
		return;
	int mid;
	mid = (begin + end) / 2;
    arr[mid] = 10 + rand() % 100;//随机10-100
	if (T == NULL)
	{
		T = (pBtree)malloc(sizeof(BTree));
		T->data = arr[mid];
		T->leftcd = NULL;
		T->rightcd = NULL;
	}
	creat(T->leftcd, arr, begin, mid - 1);
	creat(T->rightcd, arr, mid + 1, end);
}

void qianxu(pBtree T)
{
	if (T == NULL)
		return;
	cout << T->data << " ";
	qianxu(T->leftcd);
	qianxu(T->rightcd);
}
void zhongxu(pBtree T)
{
	if (T == NULL)
		return;
	zhongxu(T->leftcd);
	cout << T->data<<" ";
	zhongxu(T->rightcd);
}
/*637	Average of Levels in Binary Tree
分层遍历二叉树(按层次从上到下,从左往右) 算出每层平均值
相当于广度优先搜素,使用队列实现。
队列初始化,将跟节点压入队列。
当队列不为空:弹出一个节点,访问,若左子树节点或者右子树节点不为空,将其压入队列!
C++队列Queue类成员函数如下:
back()
返回最后一个元素
empty()
如果队列空则返回真
front()
返回第一个元素
pop()
删除第一个元素
push()
在末尾加入一个元素
size()
返回队列中元素的个数
*/
void level(pBtree root)
{
	queue<pBtree> q;
	q.push(root);
	while (!q.empty())
	{
		double total = 0.0;
		int n = q.size();
		for (int i = 0; i < n; i++)
		{
			pBtree p = q.front();
			q.pop();
			total += p->data;
			if (p->leftcd != NULL)
				q.push(p->leftcd);
			if (p->rightcd != NULL)
				q.push(p->rightcd);
		}
		cout << "当前层均值" << total / n << endl;
	}
}
/*
623. Add One Row to Tree(Difficulty: Medium)中间加一行
*/
pBtree AddRow(pBtree root,int v,int d)
{
	if(root==NULL)

		return NULL;
	if (d == 1)
	{
		pBtree t;
		t = (pBtree)malloc(sizeof(BTree));
		t->data = v;
		t->leftcd = root;
		t->rightcd = NULL;
		return t;
	}
	else if (d == 2)
	{
		pBtree left, right;
		left = (pBtree)malloc(sizeof(pBtree));
		right=(pBtree)malloc(sizeof(pBtree));
		left->leftcd = NULL;
		left->rightcd = NULL;
		right->leftcd = NULL;
		right->rightcd = NULL;
		left->data = v; right->data = v;
		left->leftcd = root->leftcd;
		root->leftcd = left;
		right->rightcd = root->rightcd;
		root->rightcd = right;
		return root;
	}
	else if(d>2)
	{
		root->leftcd=AddRow(root->leftcd,v,d-1);
		root->rightcd=AddRow(root->rightcd,v,d-1);
		return root;
	}
}
/*	617	Merge Two Binary Trees
*/
pBtree Merge(pBtree TreeA,pBtree TreeB)
{
	
	if (TreeA == NULL) return TreeB;
	if (TreeB == NULL) return TreeA;
	TreeA->data += TreeB->data;
	TreeA->leftcd = Merge(TreeA->leftcd,TreeB->leftcd);
	TreeA->rightcd = Merge(TreeA->rightcd, TreeB->rightcd);
	return TreeA;
}
int main()
{
	const int N = 10;
	int arr[N] ;
	pBtree T = NULL;
	pBtree T2 = NULL;
	creat(T, arr, 0, N - 1);
	creat(T2, arr, 0, N - 1);


	cout << "第一组"<<endl;
	cout << "前序输出";
	qianxu(T);
	cout << endl;
	cout << "中序输出";
	zhongxu(T);
	cout << endl;

	cout << "第二组" << endl;
	cout << "前序输出";
	qianxu(T2);
	cout << endl;
	cout << "中序输出";
	zhongxu(T2);
	cout << endl;
	cout << "按层次遍历" << endl;
	level(T2);

	cout << "增加一行以后第一组输出" << endl;
	T=AddRow(T,2,1);
    cout << "前序输出";
	qianxu(T);
	cout << endl;
	cout << "中序输出";
	zhongxu(T);
	cout << endl;
	cout << "按层次遍历" << endl;
	level(T);


	cout << "合并两组以后输出"<<endl;
	T2=Merge(T,T2);
	cout << "前序输出";
	qianxu(T2);
	cout << endl;
	cout << "中序输出";
	zhongxu(T2);
	cout << endl;
	level(T2);



	return 0;
}





                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值