四、二叉树

树是常用的数据存储方式,由于树中存在大量的指针结构,所以树的有关操作相对来说是比较难的。

一、 树的定义

这里用二叉树来举例子

复制代码
使用结构体的方式实现二叉树:
struct BinaryTreeNode {
	int data;
	BinartTreeNode* left;
	BinartTreeNode* right;
};

使用类的方式实验二叉树:
class BinaryTreeNode {
public:
	int data;
	BinaryTreeNode *left;
	BinaryTreeNode* right
	BinaryTreeNode(int value) : data(value), left(nullptr), right(nullptr) { }
};

二、二叉树的前、中、后序遍历

前序遍历:根左右

中序遍历:左跟右

后序遍历:左右根

2.1 前序遍历

2.1.1 递归形式实现前序遍历

c++ 复制代码
class Solution{
public:
	void preorder(TreeNode *root, vector<int> &res){
		if (root == nullptr){
			return;
		}
		res.push_back(root->val);
		preorder(root->left, res);
		preorder(root->right, res);
	}
	vector<int> preorderTraversal(TreeNode *root){
		vector<int> res;
		preorder(root, res);
		return res;
	}
};

2.1.2 迭代形式实现前序遍历

c++ 复制代码
class Solution{
public:
    vector<int> preorderTraversal(TreeNode* root){
        vector<int> res;
        if (root == nullptr){
            return res;
        }
        stack<TreeNode*> stk;//创建一个栈
        TreeNode* node =root;//创建一个树节点
        while (!stk.empty() || node != nullptr){
            while(node != nullptr){//只要节点有左子树,就进行这一步骤
                res.emplace_back(node->val);//节点的值存入res
                stk.emplace(node);//节点存入栈中
                node = node->left;
            }
            node = stk.top();
            stk.pop();
            node = node->right;
        }
        return res;
    }
};

2.2 中序遍历

2.2.1 递归形式的中序遍历

先找左子树,左子树没有了,存储当前节点,再找右子树

c++ 复制代码
class Solution {
public:
    void midorder(TreeNode *root, vector<int> &res){
        if (root == nullptr){
            return;
        }
        midorder(root->left, res);
        res.push_back(root->val);
        midorder(root->right, res);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        midorder(root, res);
        return res;
    }
};

2.2.2 迭代形式的中序遍历

c++ 复制代码
class Solution{
public:
    vector<int> inorderTraversal(TreeNode* root){
        vector<int> res;
        if(root == nullptr) return res;
        stack<TreeNode*> stk;
        TreeNode* node = root;
        while (!stk.empty() || node != nullptr){
            while (node != nullptr){
                stk.emplace(node);
                node = node->left;
            }
        node = stk.top();
        stk.pop();
        res.push_back(node->val);
        node = node->right;
        }
        return res;
    }
};

2.3 后序遍历

2.3.1 递归形式的后序遍历

c++ 复制代码
class Solution {
public:
    void lastorder(TreeNode* root, vector<int> &res)
    {
        if (root==nullptr) return;
        lastorder(root->left, res);
        lastorder(root->right, res);
        res.push_back(root->val);
    }
        vector<int> postorderTraversal(TreeNode*root) 
    {
        vector<int> res;
        lastorder(root, res);
        return res;
    }
};

2.3.2 迭代形式的后序遍历

c++ 复制代码
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        if (root == nullptr){
            return res;
        }
        stack<TreeNode *> stk;
        TreeNode *prev = nullptr;
        while (root != nullptr || !stk.empty()){
            while (root != nullptr){
                stk.emplace(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            if (root->right == nullptr || root->right == prev){
                res.emplace_back(root->val);
                prev = root;
                root = nullptr;
            }
            else{
                stk.emplace(root);
                root = root->right;
            }
        }
        return res;
    }
};

2.4 根据前序遍历构造二叉搜索树


这道题目是有一些难度的,首先需要弄明白原理,根据题目的分析,首先第一个节点是根节点(称其为root),第一个比根节点大的,就是根节点的右子树(称其为Rchild),那么root和Rchild之间的成员,一定是根节点左子树或左子树的孩子。其中的第一个,就是根节点的左子树。

那么根据上面的思想,可以写出伪代码

c++ 复制代码
TreeNode* fun(vector<int>& preorder, int left, int right){
	if (left>right) return nullptr;
	int root = preorder[left];
	int mid = left;
	for(; mid<=right; mid++){
		if(preorder[mid]>root) break;
	}
	TreeNode *node = new TreeNode(root);
	node->left = fun(vector<int>& preorder, int left+1, int mid-1)
	node->right = fun(vector<int>& preorder, int mid, int right)
	return node;
}

上面的代码还是挺难想的,必须清楚的考虑边界条件才能写对,了解一下吧

相关推荐
代钦塔拉3 分钟前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式
青少儿编程课堂13 分钟前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C/C++考级:任意进制互转与大数多进制加法解析
c++·python·算法·bfs·信息学竞赛
Shadow(⊙o⊙)18 分钟前
仿muduo实现高并发服务器——EPOLL设置与移除Poller.hpp管理Channel
运维·服务器·c++
闻缺陷则喜何志丹31 分钟前
【C++特性及类库】多线程
开发语言·c++·多线程·语言特性·系统库
多弗朗皮卡丘31 分钟前
C++ 哈希(下)
c++·算法·哈希算法
dtq042444 分钟前
C++3 - 分支语句if和string变量
开发语言·c++·学习
jimy144 分钟前
c++类成员访问只看静态类型
开发语言·c++
CCCCCCCCharlie1 小时前
从0到1:一个字节的磁盘之旅
linux·开发语言·c++
艾莉丝努力练剑1 小时前
【AI大模型接入SDK】ChatServer接口实现
c++·人工智能·学习·ai·架构·大模型·sdk
要练八块腹肌1 小时前
AVL树的讲解与实现
c++