二叉树相关操作

时间:2022-03-03 14:37:54

LeetCode:先序遍历
代码:
递归:

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

非递归:

/** * 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> v;
        stack<TreeNode*> s;
        while(root || !s.empty()){
            while(root){
                v.push_back(root->val);
                s.push(root);
                root = root->left;
            }
            if(!s.empty()){
                root = s.top();
                s.pop();
                root = root->right;
            }
        }
        return v;
    }
};

中序遍历:https://leetcode.com/problems/binary-tree-inorder-traversal/
递归:

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

非递归:

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

后序遍历:https://leetcode.com/problems/binary-tree-postorder-traversal/description/
递归:

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

非递归:

/** * 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> postorderTraversal(TreeNode* root) {
        vector<int> v;
        stack<TreeNode*> s;
         TreeNode *pre = NULL,*t;
        while(root || !s.empty()){
            while(root){
                s.push(root);
                root = root->left;
            }
            if(!s.empty()){
                t = s.top();
                if(t->right != pre){
                    root = t->right;
                    pre = NULL;
                }
                else{
                    v.push_back(t->val);
                    pre = t;
                    s.pop();
                }
            }

        }
        return v;
    }
};
/** * 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> postorderTraversal(TreeNode* root) {
        vector<int> v;
        stack<TreeNode*> s;
        if(root)
            s.push(root);
        TreeNode *pre = root,*t = NULL;
        while(!s.empty()){
            t = s.top();
            if((t->left == NULL && t->right == NULL) || t->left == pre || t->right == pre){
                v.push_back(t->val);
                pre = t;
                s.pop();
            }
            else{
                if(t->right)
                    s.push(t->right);
                if(t->left)
                    s.push(t->left);
            }
        }
        return v;
    }
};