zoukankan      html  css  js  c++  java
  • LeetCode94二叉树中序遍历

    题目链接

    https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

    题解一:递归

    // Problem: LeetCode 94
    // URL: https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
    // Tags: Tree Recursion Stack
    // Difficulty: Medium
    
    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct TreeNode{
        int val;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int x):val(x), left(nullptr), right(nullptr){}
    };
    
    class Solution {
    public:
        void traversal(TreeNode* root, vector<int>& result){
            if(root!=nullptr){
                traversal(root->left, result);
                result.push_back(root->val);
                traversal(root->right, result);
            }
        }
    
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> result;
            traversal(root, result);
            return result;
        }
    };
    
    int main()
    {
        cout << "helloworld" << endl;
        // system("pause");
        return 0;
    }
    

    题解二:非递归(通过栈模拟递归)

    思路:https://www.cnblogs.com/chouxianyu/p/13293284.html

    // Problem: LeetCode 94
    // URL: https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
    // Tags: Tree Recursion Stack
    // Difficulty: Medium
    
    #include <iostream>
    #include <vector>
    #include <stack>
    using namespace std;
    
    struct TreeNode{
        int val;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int x):val(x), left(nullptr), right(nullptr){}
    };
    
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> result;
            stack<TreeNode*> nodes;
            if(root!=nullptr)
                nodes.push(root);
            while(!nodes.empty()){
                root = nodes.top();
                nodes.pop();
                // 模拟调用递归函数
                if(root!=nullptr){
                    if(root->right!=nullptr)
                        nodes.push(root->right);
                    nodes.push(root);
                    nodes.push(nullptr);
                    if(root->left!=nullptr)
                        nodes.push(root->left);
                }
                // 一层递归函数结束
                else{
                    result.push_back(nodes.top()->val);
                    nodes.pop();
                }
            }
            return result;
        }
    };
    
    int main()
    {
        cout << "helloworld" << endl;
        // system("pause");
        return 0;
    }
    

    作者:@臭咸鱼

    转载请注明出处:https://www.cnblogs.com/chouxianyu/

    欢迎讨论和交流!


  • 相关阅读:
    layui 动态设置radio选中
    C# ling 查询 in 用法
    sql 去除小数点后面无效的0
    VUE 全局变量申明和取值
    SQL 逗号分隔将一行拆成多行
    devexpress 延长试用期 licenses.licx
    BugkuCTF-WEB4
    一招破解网页复制+网页上如何实现禁止复制粘贴
    JS的函数
    JS的数组
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/13293153.html
Copyright © 2011-2022 走看看