【C++编程基础】#010 二叉树的前序、中序、后序遍历非递归方法,得到最小树高度。

问题描述

本文介绍了一些二叉树的“进阶”操作,以C++语言实现,主要包括以下功能:

  1. 二叉树的前序遍历非递归方法
  2. 二叉树的中序遍历非递归方法
  3. 二叉树的后序遍历非递归方法
  4. 得到二叉树的最小树高

小伙伴们需要自取,有帮助的话可以点个赞~
注:本文中提供的代码均为完整代码,放入C++工程后可直接运行。

代码实现

用C++类(class)实现,共包含三个文件:

  1. solution.h 函数体声明
  2. solution.cpp 函数实现
  3. main.cpp 测试代码

文件一 、solution.h

#ifndef SOLUTION_H
#define SOLUTION_H
#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
	char val;
	ListNode* lchild;
	ListNode* rchild;
	ListNode(int x) :val(x), lchild(NULL),rchild(NULL) {};
};

class Solution {
public:

	vector<char>inorderTraversal(ListNode*);	
	void createTree(ListNode*&);				//创建二叉树
	void preTraversal(ListNode*);				//前序遍历的非递归方法
	void midTraversal(ListNode*);				//中序遍历的非递归方法
	void postTraversal(ListNode*);				//后序遍历的非递归方法
	int getLength(ListNode*);					//得到树高
	int minDepth(ListNode*,int &height);		//得到二叉树的最小树高

private:
	vector<char>a;
};

#endif // !SOLUTION_H

文件二、solution.cpp



#include<iostream>
#include"solution.h"
#include<stack>
#include<queue>
using namespace std;

vector<char> Solution::inorderTraversal(ListNode* root)
{

	if (root == NULL)
		return a;
	else
	{
		if (root->lchild != NULL)
		{
			inorderTraversal(root->lchild);
		}
		a.push_back(root->val);
		if (root->rchild != NULL)
		{
			inorderTraversal(root->rchild);
		}
	}
	return a;

}
void Solution::createTree(ListNode* &root)
{
	char word;
	cin >> word;

	if (word == '0')
	{
		root == NULL;
	}

	else
	{
		root = new ListNode(0);
		root->val = word;
		createTree(root->lchild);
		createTree(root->rchild);
	}
}


void Solution::preTraversal(ListNode* root)
{
	if (root == NULL)
		return;
	stack<ListNode*>stk;
	stk.push(root);
	while (!stk.empty())
	{
		ListNode* temp = stk.top();
		cout << temp->val << " ";
		stk.pop();
		if (temp->rchild)
			stk.push(temp->rchild);
		if (temp->lchild)
			stk.push(temp->lchild);
	}
}

void Solution::midTraversal(ListNode* root)
{
	stack<ListNode*>stk;
	ListNode* temp = root;
	while (temp || !stk.empty())
	{
		if (temp)
		{
			stk.push(temp);
			temp = temp->lchild;
		}
		else
		{
			temp = stk.top();
			cout << temp->val << " ";
			stk.pop();
			temp = temp->rchild;
		}
	}
}

void Solution::postTraversal(ListNode* root)
{
	if (root == NULL)
		return;
	stack<ListNode*> stk1;
	stack<ListNode*> stk2;
	stk1.push(root);

	while (!stk1.empty())
	{
		ListNode*temp = stk1.top();
		stk1.pop();
		stk2.push(temp);
		if (temp->lchild)
			stk1.push(temp->lchild);
		if (temp->rchild)
			stk1.push(temp->rchild);
	}
	while (!stk2.empty())
	{
		cout << stk2.top()->val << " ";
		stk2.pop();
	}
}

int Solution::minDepth(ListNode* root,int &height)
{
	if (root->lchild != NULL)
	{
		if (getLength(root) > getLength(root->lchild) + 1)
		{
			height--;
			minDepth(root->lchild, height);
		}
	}
	if (root->rchild != NULL)
	{
		if (getLength(root) > getLength(root->rchild) + 1)
		{
			height--;
			minDepth(root->rchild, height);
		}
	}
	if (root->rchild != NULL && root->lchild != NULL)
	{
		if (getLength(root) == getLength(root->rchild) + 1 && getLength(root) == getLength(root->lchild) + 1)
		{
			height--;
			minDepth(root->rchild, height);
			minDepth(root->lchild, height);
		}
	}

	return height;
}


int Solution::getLength(ListNode* root)
{
	if (root == NULL)
		return 0;
	int height = 0;
	queue<ListNode*>que;
	que.push(root);
	ListNode* last = root;

	while (!que.empty())
	{
		ListNode* temp = que.front();
		if (temp != NULL)
		{
			que.pop();
			if (temp->lchild != NULL)
				que.push(temp->lchild);
			if (temp->rchild != NULL)
				que.push(temp->rchild);
			if (temp == last)
			{
				if (!que.empty())
				{
					last = que.back();
				}
				height++;
			}
		}
	}
	return height;
}

文件三、main.cpp



#include<iostream>
#include<vector>
#include"solution.h"
using namespace std;
int main()
{
	Solution solution;
	ListNode* root = new ListNode(0);
	solution.createTree(root);

	int height = solution.getLength(root);
	cout << solution.minDepth(root,height);
	cout << endl;

	system("pause");
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++数据结构中树的二叉树前序中序后序遍历的递归与非递归代码,包括输入与输出。 假设二叉树的节点定义如下: ``` struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(NULL), right(NULL) {} }; ``` 前序遍历: 递归版本: ``` void preorderTraversal(Node* root) { if (!root) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } ``` 非递归版本: ``` void preorderTraversal(Node* root) { stack<Node*> s; s.push(root); while (!s.empty()) { Node* node = s.top(); s.pop(); if (!node) continue; cout << node->val << " "; s.push(node->right); s.push(node->left); } } ``` 中序遍历: 递归版本: ``` void inorderTraversal(Node* root) { if (!root) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } ``` 非递归版本: ``` void inorderTraversal(Node* root) { stack<Node*> s; Node* node = root; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 后序遍历: 递归版本: ``` void postorderTraversal(Node* root) { if (!root) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } ``` 非递归版本: ``` void postorderTraversal(Node* root) { stack<Node*> s; Node* node = root; Node* last = NULL; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); if (!node->right || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = NULL; } else { node = node->right; } } } ``` 输入与输出: ``` int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "前序遍历(递归): "; preorderTraversal(root); cout << endl; cout << "前序遍历(非递归): "; preorderTraversal(root); cout << endl; cout << "中序遍历(递归): "; inorderTraversal(root); cout << endl; cout << "中序遍历(非递归): "; inorderTraversal(root); cout << endl; cout << "后序遍历(递归): "; postorderTraversal(root); cout << endl; cout << "后序遍历(非递归): "; postorderTraversal(root); cout << endl; return 0; } ``` 输出结果: ``` 前序遍历(递归): 1 2 4 5 3 6 7 前序遍历(非递归): 1 2 4 5 3 6 7 中序遍历(递归): 4 2 5 1 6 3 7 中序遍历(非递归): 4 2 5 1 6 3 7 后序遍历(递归): 4 5 2 6 7 3 1 后序遍历(非递归): 4 5 2 6 7 3 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值