1. 程式人生 > >一篇文章解決所有LeetCode樹的問題

一篇文章解決所有LeetCode樹的問題

// 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;
}