[leetcode]树的操作

一.定义二叉树的结构

// 定义二叉树结构

typedef struct node {

char ch;

node* LChild;

node* RChild;

} TiTNode, * BiTree;

二.创建BiTree

// 创建BiTree

void createTree(BiTree& bt) {

char ch;

cin >> ch;

if (ch == '#') {

bt = NULL;

}

else {

bt = new node;

bt->ch = ch;

createTree(bt->LChild);

createTree(bt->RChild);

}

}

三.访问节点数据

// 访问节点数据

void visit(char ch) {

cout << ch << " ";

}

四.先序遍历

// 先序遍历

void PreOrder(BiTree root) {

if (root != NULL) {

visit(root->ch);

PreOrder(root->LChild);

PreOrder(root->RChild);

}

}

五. 中序遍历

// 中序遍历

void InOrder(BiTree root) {

if (root != NULL) {

InOrder(root->LChild);

visit(root->ch);

InOrder(root->RChild);

}

}

六.后序遍历

// 后序遍历

void PostOrder(BiTree root) {

if (root != NULL) {

PostOrder(root->LChild);

PostOrder(root->RChild);

visit(root->ch);

}

}

七.后序遍历求二叉树的高度

// 后序遍历求二叉树的高度

int PostTreeDepth(BiTree bt) {

if (bt == NULL) {

return 0;

}

int hl = PostTreeDepth(bt->LChild);

int hr = PostTreeDepth(bt->RChild);

return (hl > hr ? hl : hr) + 1;

}

八.先序遍历求二叉树的高度

// 先序遍历求二叉树的高度

int PreTreeDepth(BiTree bt) {

if (bt == NULL) {

return 0;

}

int hl = PreTreeDepth(bt->LChild);

int hr = PreTreeDepth(bt->RChild);

return (hl > hr ? hl : hr) + 1;

}

九.计算二叉树的叶子节点的个数

//计算二叉树的叶子节点的个数

void caculateLeaves(BiTree bt,int* leaf)

{

if (bt != NULL)

{

if (bt->LChild == NULL && bt->RChild == NULL)

{

(*leaf)++;

}

caculateLeaves(bt->LChild, leaf);

caculateLeaves(bt->RChild, leaf);

}

else

{

return;

}

}

Coding:

#include <iostream>

using namespace std;

// 定义二叉树结构

typedef struct node {

char ch;

node* LChild;

node* RChild;

} TiTNode, * BiTree;

// 创建BiTree

void createTree(BiTree& bt) {

char ch;

cin >> ch;

if (ch == '#') {

bt = NULL;

}

else {

bt = new node;

bt->ch = ch;

createTree(bt->LChild);

createTree(bt->RChild);

}

}

// 访问节点数据

void visit(char ch) {

cout << ch << " ";

}

// 先序遍历

void PreOrder(BiTree root) {

if (root != NULL) {

visit(root->ch);

PreOrder(root->LChild);

PreOrder(root->RChild);

}

}

// 中序遍历

void InOrder(BiTree root) {

if (root != NULL) {

InOrder(root->LChild);

visit(root->ch);

InOrder(root->RChild);

}

}

// 后序遍历

void PostOrder(BiTree root) {

if (root != NULL) {

PostOrder(root->LChild);

PostOrder(root->RChild);

visit(root->ch);

}

}

// 后序遍历求二叉树的高度

int PostTreeDepth(BiTree bt) {

if (bt == NULL) {

return 0;

}

int hl = PostTreeDepth(bt->LChild);

int hr = PostTreeDepth(bt->RChild);

return (hl > hr ? hl : hr) + 1;

}

// 先序遍历求二叉树的高度

int PreTreeDepth(BiTree bt) {

if (bt == NULL) {

return 0;

}

int hl = PreTreeDepth(bt->LChild);

int hr = PreTreeDepth(bt->RChild);

return (hl > hr ? hl : hr) + 1;

}

//计算二叉树的叶子节点的个数

void caculateLeaves(BiTree bt,int* leaf)

{

if (bt != NULL)

{

if (bt->LChild == NULL && bt->RChild == NULL)

{

(*leaf)++;

}

caculateLeaves(bt->LChild, leaf);

caculateLeaves(bt->RChild, leaf);

}

else

{

return;

}

}

int main() {

// 示例输入:ABD##E##CF###

BiTree root = NULL;

cout << "Enter the tree structure (use # for null nodes): ";

createTree(root);

cout << "PreOrder: ";

PreOrder(root);

cout << endl;

cout << "InOrder: ";

InOrder(root);

cout << endl;

cout << "PostOrder: ";

PostOrder(root);

cout << endl;

int height = PostTreeDepth(root);

cout << "The height of the tree (PostOrder): " << height << endl;

height = PreTreeDepth(root);

cout << "The height of the tree (PreOrder): " << height << endl;

int leaf = 0;

caculateLeaves(root,&leaf);

cout << "The number of leaves is : " << leaf << endl;

return 0;

}

相关推荐
王中阳Go2 小时前
从超市收银到航空调度:贪心算法如何破解生活中的最优决策谜题?
java·后端·算法
故事挺秃然3 小时前
中文分词:机械分词算法详解与实践总结
算法·nlp
车队老哥记录生活5 小时前
【MPC】模型预测控制笔记 (3):无约束输出反馈MPC
笔记·算法
地平线开发者5 小时前
BEV 感知算法评价指标简介
算法·自动驾驶
不过四级不改名6776 小时前
用c语言实现简易c语言扫雷游戏
c语言·算法·游戏
C++ 老炮儿的技术栈7 小时前
手动实现strcpy
c语言·开发语言·c++·算法·visual studio
倔强的石头_8 小时前
【数据结构与算法】利用堆结构高效解决TopK问题
后端·算法
倔强的石头_8 小时前
【数据结构与算法】详解二叉树下:实践篇————通过链式结构深入理解并实现二叉树
后端·算法
哎写bug的程序员8 小时前
leetcode复盘(1)
算法·leetcode·职场和发展
风靡晚8 小时前
用于汽车毫米波雷达的四维高分辨率点云图像
人工智能·算法·机器学习·计算机视觉·汽车·信息与通信·信号处理