zoukankan      html  css  js  c++  java
  • LeetCode145二叉树后序遍历

    题目链接

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

    题解一:递归

    // Problem: LeetCode 145
    // URL: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
    // Tags: Stack Tree Recursion 
    // Difficulty: Hard
    
    #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:
        vector<int> result;
    
        void traversal(TreeNode* root){
            if(root!=nullptr){
                if(root->left!=nullptr)
                    traversal(root->left);
                if(root->right!=nullptr)
                    traversal(root->right);
                result.push_back(root->val);
            }
        }
    
        vector<int> postorderTraversal(TreeNode* root){
            traversal(root);
            return result;
        }
    };
    
    int main()
    {
        cout << "helloworld" << endl;
        // system("pause");
        return 0;
    }
    

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

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

    // Problem: LeetCode 145
    // URL: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
    // Tags: Stack Tree Recursion 
    // Difficulty: Hard
    
    #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> postorderTraversal(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){
                    nodes.push(root);
                    nodes.push(nullptr);
                    if(root->right!=nullptr)
                        nodes.push(root->right);
                    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/

    欢迎讨论和交流!


  • 相关阅读:
    05 drf源码剖析之认证
    04 drf源码剖析之版本
    03 drf源码剖析之视图
    02 drf源码剖析之快速了解drf
    OA之为用户设置角色和为用户设置权限
    EasyUI之datagrid的使用
    C#之反射
    EasyUI之Layout布局和Tabs页签的使用
    Memcached的使用
    Log4Net的简单使用
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/13293152.html
Copyright © 2011-2022 走看看