Leetcode 94 二叉树的中序遍历

在这里插入图片描述

方法1、暴力递归
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
    void inorder(TreeNode *root, vector<int> &res) {
        if (root != nullptr) {
            inorder(root->left, res);
            res.push_back(root->val);
            inorder(root->right, res);
        }
    }
};
方法 2、颜色标记法
  • 这里每个结点都遍历两次!

  • 使用栈来深度遍历二叉树,只有非空节点才压入栈中 非空节点才压入栈中 非空节点才压入栈中

  • 这里需要用到三种数据结构

    1. vector
      • #include <vector>
      • 添加元素 push_back()
    2. stack
      • #include <stack>
      • 添加元素(压栈) push() 栈顶元素 top() 删除元素(出栈) pop()
    3. pair 对于pair一定要消除一个误区,pair跟map没有绝对关系,并不是在map下才有pair。由于是学map时学的pair给人一种错觉
      • #include <utility>
      • 创建pair的两种方法
      //方法1  跟所有类创建对象一样,也就是使用构造函数
      pair<TreeNode *, int> my_pair(root, 1);
      pair<TreeNode *, int>(root, 1); //匿名pair对象
      //方法2  使用make_pair函数模板,其实就是利用拷贝构造函数!
      pair<TreeNode *, int> my_pair = make_pair(root, 1);
      make_pair(root, 1); //匿名pair对象
    
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        std::vector<int> res;
        if(root == NULL)
            return res;
        
        std::stack<pair<TreeNode *, int>> stk;
        stk.push(pair<TreeNode *, int>(root, 1));

        while(!stk.empty()){
            pair<TreeNode *, int> tmp = stk.top();
            stk.pop();
            if(tmp.second == 0)
                res.push_back(tmp.first->val);
            else{
                if(tmp.first->right != NULL) // 非空节点才压入栈中
                    stk.push(pair<TreeNode *, int>(tmp.first->right, 1));

                stk.push(pair<TreeNode *,int>(tmp.first, 0));

                if(tmp.first->left != NULL)  //非空节点才压入栈中
                    stk.push(pair<TreeNode *, int>(tmp.first->left, 1));
            }
        }

        return res;
    }
};
方法3、左链入栈
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode *> stk;

        auto p = root;

        while (p) {
            stk.push(p);
            p = p->left;
        }

        while (!stk.empty()) {
            auto tmp = stk.top();
            stk.pop();

            res.push_back(tmp->val);

            p = tmp->right;
            while (p) {
                stk.push(p);
                p = p->left;
            }
        }

        return res;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == nullptr) return res;
        stack<TreeNode *> stk;
        auto p = root;
        
        while (p != nullptr || !stk.empty()) {
            while (p != nullptr) {
                stk.push(p);
                p = p->left;
            }

            auto tmp = stk.top();
            stk.pop();

            res.push_back(tmp->val);
            p = tmp->right;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值