// 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;
}
一篇文章解决所有LeetCode树的问题
最新推荐文章于 2024-04-07 12:35:44 发布