代码随想录算法训练营第十四天 |LC 144. 二叉树的前序遍历、LC 94. 二叉树的中序遍历、LC 145. 二叉树的后序遍历

二叉树理论

二叉树

分类

  • 满二叉树
  • 完全二叉树
  • 二叉搜索树
  • 平衡二叉搜索树:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。C++中map、set、multimap,multiset的底层实现都是平衡二叉搜索树,所以map、set的增删操作时间时间复杂度是logn,注意我这里没有说unordered_map、unordered_set,unordered_map、unordered_set底层实现是哈希表。

存储方式

  • 链式
  • 数组 2i+1 2i+2

结构体

struct Treenode{
	int val;
	Treenode* left;
	Treenode* right;
	Treenode(int x): val(x), left(nullptr), right(nullptr){}
};

144. 二叉树的前序遍历

思路

  1. 递归
    截止条件->单层递归逻辑
  2. 迭代
    中左右 访问顺序和存储顺序是一致的,栈中放入顺序是右左,pop的时候才是左右。

代码

  1. 递归
class Solution {
public:
    vector<int> res;
    void Traversal(TreeNode* root){
        if (!root) return;
        res.push_back(root->val);
        Traversal(root->left);
        Traversal(root->right);
        return;
    }
    vector<int> preorderTraversal(TreeNode* root) {
        Traversal(root);
        return res;
    }
};
  1. 迭代
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> res;
        if (root == nullptr) return {};
        stk.push(root);
        while (!stk.empty()){
            auto a = stk.top();
            stk.pop();
            res.push_back(a->val);
            if (a->right) stk.push(a->right);
            if (a->left) stk.push(a->left);
        }
        return res;
    }
};

94. 二叉树的中序遍历

思路

  1. 递归
    截止条件->单层递归逻辑
  2. 迭代
    左中右,访问顺序是中开始,但是存储是最左侧的,所以需要一个指针指向现在的root节点。

代码

  1. 递归
class Solution {
public:
    vector<int> res;
    void Traversal(TreeNode* root){
        if (root == nullptr) return;
        Traversal(root->left);
        res.push_back(root->val);
        Traversal(root->right);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        Traversal(root);
        return res;
    }
};
  1. 迭代
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> res;
        if (root == nullptr) return res;
        TreeNode* cur = root;
        while (cur != nullptr || !stk.empty()){
            if (cur){
                stk.push(cur);
                cur = cur->left; // 左
            }else{
                cur = stk.top();
                stk.pop();
                res.push_back(cur->val); // 中
                cur = cur->right; // 右
            }
        }
        return res;
    }
};

145. 二叉树的后序遍历

思路

  1. 递归
    截止条件->单层递归逻辑
  2. 迭代
    左右中,中右左,左右中

代码

  1. 递归
class Solution {
public:
    vector<int> res;
    void Traversal(TreeNode* root){
        if (!root) return;
        Traversal(root->left);
        Traversal(root->right);
        res.push_back(root->val);
        return;
    }
    vector<int> postorderTraversal(TreeNode* root) {
        Traversal(root);
        return res;
    }
};
  1. 迭代
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> res;
        if (root == nullptr) return res;
        stk.push(root);
        while (!stk.empty()){
            auto a = stk.top();
            stk.pop();
            res.push_back(a->val);
            if (a->left) stk.push(a->left);
            if (a->right) stk.push(a->right);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值