代码随想录算法训练营第14天|二叉树理论基础、递归遍历、迭代遍历、统一迭代遍历

二叉树理论基础


二叉树递归遍历

题目链接:前序遍历 中序遍历 后序遍历
文章讲解:link
视频讲解:link

如何书写递归函数?

  • 确定递归函数的参数和返回值
  • 确定终止条件
  • 确定单层递归的逻辑

二叉树迭代遍历

题目链接:同上
文章讲解:link
视频讲解:先序和后序 中序

一、做题感受&第一想法

知道手算方法,但是写成代码有困难。没写出来。

二、学习文章后收获

1.中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while(cur != NULL || !st.empty()){
            if(cur != NULL){
                st.push(cur);
                cur = cur->left;
            }
            else{
                cur = st.top();
                st.pop();
                result.push_back(cur->val);
                cur = cur->right;
            }
        }
        return result;
    }
};

2.先序遍历

最简单,因为遍历顺序和处理顺序相同。

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur = NULL;
        st.push(root);
        while( !st.empty() ){
            cur = st.top();
            st.pop();
            if(cur != NULL){
                result.push_back(cur->val);
                st.push(cur->right);
                st.push(cur->left);
            }
        }
        return result;
    }
};

3.后序遍历

先序遍历是“根左右”,后序遍历是“左右根”。那么可以先按“根右左”遍历,再reverse翻转一下。

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur = NULL;
        st.push(root);
        while( !st.empty() ){
            cur = st.top();
            st.pop();
            if(cur != NULL){
                result.push_back(cur->val);
                st.push(cur->left);
                st.push(cur->right);
            }
        }
        reverse(result.begin(),result.end());
        return result;
    }
};

三、过程中遇到的问题

1.vector如何reverse翻转

vector没有reverse()的成员函数!

reverse(v.begin(),v.end()); //会翻转左闭右开区间

二叉树统一迭代

题目链接:同上
文章讲解:link

一、做题感受&第一想法

没有思路。

二、学习文章后收获

主要思路:NULL标识处理元素

根节点先入栈。此后栈不为空则一直循环。
每次循环时,弹出栈顶元素:
①如果栈顶元素不为NULL,则右孩子先入栈(NULL则不入栈),然后自己入栈,然后NULL入栈,然后左孩子入栈(NULL则不入栈)。
②如果栈顶元素为NULL,则说明下一个元素是需要输出的节点,那么弹出栈顶元素,把它的val加入result容器中。

1.中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur = NULL;
        if(root) st.push(root);
        while(!st.empty()){
            cur = st.top();
            st.pop();
            if(cur){ //按 右、根、左 入栈。
                if(cur->right) st.push(cur->right);
                st.push(cur);
                st.push(NULL);
                if(cur->left) st.push(cur->left);
            }
            else{
                cur = st.top();
                st.pop();
                result.push_back(cur->val);
            }
        }
        return result;
    }
};

2.先序遍历

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur;
        if(root) st.push(root);
        while(!st.empty()){
            cur = st.top();
            st.pop();
            if(cur){ //按 右、左、根 入栈。
                if(cur->right) st.push(cur->right);
                if(cur->left) st.push(cur->left);
                st.push(cur);
                st.push(NULL);
            }
            else{
                cur = st.top();
                st.pop();
                result.push_back(cur->val);
            }
        }
        return result;
    }
};

3.后序遍历

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur;
        if(root) st.push(root);
        while(!st.empty()){
            cur = st.top();
            st.pop();
            if(cur){ //按 根、右、左 入栈。
                st.push(cur);
                st.push(NULL);
                if(cur->right) st.push(cur->right);
                if(cur->left) st.push(cur->left);
            }
            else{
                cur = st.top();
                st.pop();
                result.push_back(cur->val);
            }
        }
        return result;
    }
};

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值