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

}

相关推荐
大数据张老师3 分钟前
数据结构——BF算法
数据结构·算法·1024程序员节
让我们一起加油好吗13 分钟前
【数论】欧拉定理 && 扩展欧拉定理
c++·算法·数论·1024程序员节·欧拉定理·欧拉降幂·扩展欧拉定理
一匹电信狗27 分钟前
【LeetCode_876_2.02】快慢指针在链表中的简单应用
c语言·数据结构·c++·算法·leetcode·链表·stl
胖咕噜的稞达鸭30 分钟前
算法入门---专题二:滑动窗口2(最大连续1的个数,无重复字符的最长子串 )
c语言·数据结构·c++·算法·推荐算法·1024程序员节
兮山与33 分钟前
算法18.0
算法
码农多耕地呗1 小时前
力扣543.二叉树的直径(java)(迭代法 and 左右根后序遍历迭代法)
算法·leetcode·职场和发展
努力学习的小廉2 小时前
我爱学算法之—— 分治-归并
c++·算法·1024程序员节
Gorgous—l2 小时前
数据结构算法学习:LeetCode热题100-链表篇(下)(随机链表的复制、排序链表、合并 K 个升序链表、LRU 缓存)
数据结构·学习·算法
仰泳的熊猫2 小时前
LeetCode:200. 岛屿数量
数据结构·c++·算法·leetcode
流星5211222 小时前
GC 如何判断对象该回收?从可达性分析到回收时机的关键逻辑
java·jvm·笔记·学习·算法