94、144、145,二叉树遍历

前序遍历

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

中序遍历

递归写法:

class Solution {
    
    
public:
    void f(TreeNode* root, vector<int> &ans)
    {
    
    
        if (root)
        {
    
    
            f(root->left, ans);
            ans.push_back(root->val);
            f(root->right, ans);
        }
    }
    vector<int> inorderTraversal(TreeNode* root) 
    {
    
    
        vector<int> ans;
        f(root, ans);
        return ans;
    }
};

非递归写法:

class Solution {
    
    
public:
    void f(TreeNode* root, vector<int> &ans)
    {
    
    
        stack<TreeNode*> s;
        TreeNode *p = root;
        while (p || !s.empty())
        {
    
    
            if (p)
            {
    
    
                s.push(p);
                p = p->left;
            }
            else
            {
    
    
                ans.push_back(s.top()->val);
                p = s.top()->right;
                s.pop();
            }
        }
    }
    vector<int> inorderTraversal(TreeNode* root) 
    {
    
    
        vector<int> ans;
        f(root, ans);
        return ans;
    }
};

后序遍历

递归写法

class Solution {
    
    
public:
    void f(TreeNode* root, vector<int> &ans)
    {
    
    
        if (root)
        {
    
    
            f(root->left, ans);
            f(root->right, ans);
            ans.push_back(root->val);
        }
    }
    vector<int> postorderTraversal(TreeNode* root) 
    {
    
    
        vector<int> ans;
        f(root, ans);
        return ans;
    }
};

非递归写法

class Solution {
    
    
public:
    void f(TreeNode* root, vector<int> &ans)
    {
    
    
        stack<TreeNode*> s;
        TreeNode *p = root, *temp = NULL;
        while (p || !s.empty())
        {
    
    
            if (p)
            {
    
    
                s.push(p);
                p = p->left;
            }
            else
            {
    
    
                p = s.top();
                if (p->right != NULL && p->right != temp) p = p->right;
                else
                {
    
    
                    ans.push_back(p->val);
                    s.pop();
                    temp = p;
                    p = NULL;
                }
            }
        }
    }
    vector<int> postorderTraversal(TreeNode* root) 
    {
    
    
        vector<int> ans;
        f(root, ans);
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/Shao_yihao/article/details/120943225
今日推荐