[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;

}

相关推荐
GSDjisidi17 分钟前
日本IT就职面试|仪容&礼仪篇分享建议
面试·职场和发展
paopaokaka_luck1 小时前
婚纱摄影管理系统(发送邮箱、腾讯地图API、物流API、webSocket实时聊天、协同过滤算法、Echarts图形化分析)
vue.js·spring boot·后端·websocket·算法·echarts
愚戏师2 小时前
机器学习(重学版)基础篇(算法与模型一)
人工智能·算法·机器学习
OEC小胖胖5 小时前
渲染篇(二):解密Diff算法:如何用“最少的操作”更新UI
前端·算法·ui·状态模式·web
找不到、了5 小时前
Java排序算法之<归并排序>
算法·排序算法
香蕉可乐荷包蛋5 小时前
排序算法 (Sorting Algorithms)-Python示例
python·算法·排序算法
Sylvia-girl5 小时前
排序查找算法,Map集合,集合的嵌套,Collections工具类
java·算法·排序算法
TT哇5 小时前
【分治】归并排序——排序数组(medium)
java·算法·排序算法
skyang.5 小时前
LeetCode 85. 最大矩形
算法·leetcode·职场和发展
滋滋不吱吱6 小时前
枚举中间位置基础篇
考研·算法·leetcode