• LeetCode144二叉树前序遍历


    题目链接

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

    题解一:递归

    // Problem: LeetCode 144
    // URL: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
    // 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){
                result.push_back(root->val);
                if(root->left!=nullptr)
                    traversal(root->left, result);
                if(root->right!=nullptr)
                    traversal(root->right, result);
            }
        }
        vector<int> preorderTraversal(TreeNode* root){
            vector<int> result;
            traversal(root, result);
            return result;
        }
    };
    
    int main()
    {
        cout << "helloworld" << endl;
        // system("pause");
        return 0;
    }
    

    题解二:非递归

    • 需要用到栈
    • 注意循环条件
    • 注意是先把右子树存入栈中
    // Problem: LeetCode 144
    // URL: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
    // 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> preorderTraversal(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){
                    result.push_back(root->val);
                    nodes.push(root->right);
                    nodes.push(root->left);
                }
            }
            return result;
        }
    };
    
    int main()
    {
        cout << "helloworld" << endl;
        // system("pause");
        return 0;
    }
    

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

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

    // Problem: LeetCode 144
    // URL: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
    // 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> preorderTraversal(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);
                    if(root->left!=nullptr)
                        nodes.push(root->left);
                    nodes.push(root);
                    nodes.push(nullptr);  // 设置nullptr标志
                }
                // nullptr代表栈顶对应的树(根、左子树、右子树)已按照指定顺序遍历,一层递归函数运行结束
                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/

    欢迎讨论和交流!


  • 相关阅读:
    秒杀多线程第八篇 经典线程同步 信号量Semaphore
    SURF特征
    (最短路径算法整理)
    中国大推力矢量发动机WS15 跨入 世界先进水平!
    SQL Profile 总结(一)
    Spring下@ResponseBody响应中文内容乱码问题
    Ubuntu12.04下jamvm1.5.4+classpath-0.98成功执行 helloworld.class
    【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!
    [数据结构] N皇后问题
    DG之主库、备库切换(物理备库)
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/13290758.html
Copyright © 2020-2023  润新知