zoukankan      html  css  js  c++  java
  • Leetcode 144

    /**
     * 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> preorderTraversal(TreeNode* root) {
            vector<int> res;
            dfs(root,res);
            return res;
        }
        void dfs(TreeNode* root,vector<int>& res){
            if(root == NULL) return;
            res.push_back(root->val);
            dfs(root->left,res);
            dfs(root->right,res);
        }
    };

    迭代遍历:

    一般我们提到树的遍历,最常见的有先序遍历,中序遍历,后序遍历和层序遍历,它们用递归实现起来都非常的简单。而题目的要求是不能使用递归求解,于是只能考虑到用非递归的方法,这就要用到stack来辅助运算。由于先序遍历的顺序是"根-左-右", 算法为:

    1. 把根节点push到栈中

    2. 循环检测栈是否为空,若不空,则取出栈顶元素,保存其值,然后看其右子节点是否存在,若存在则push到栈中。再看其左子节点,若存在,则push到栈中。

    /**
     * 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> preorderTraversal(TreeNode* root) {
            vector<int> res;
            if(root == NULL) return res;
            stack<TreeNode*> st;
            st.push(root);
            while(!st.empty()){
                TreeNode* p = st.top();
                st.pop();
                res.push_back(p->val);
                if(p->right) st.push(p->right);
                if(p->left) st.push(p->left);
            }
            return res;
        }
        
    };

    ——

  • 相关阅读:
    data object audit
    trigger dependencies
    redo allocation latch redo copy latch
    查看TEMP 表空间usage
    oracle 查看隐藏参数
    weblogic 10 无密码启动
    lead 函数和 lag函数
    oracle latch
    查看OS 各项参数
    深度学习小贴士
  • 原文地址:https://www.cnblogs.com/cunyusup/p/10562319.html
Copyright © 2011-2022 走看看