[Data structure] tree related algorithm 1

1-nearest common ancestor

Sword Finger Offer 68-II. The nearest common ancestor of the binary tree

Insert picture description here

/**
 * 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:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        if(!root || root == p || root == q) return root;

        auto left = lowestCommonAncestor(root->left,p,q);
        auto right = lowestCommonAncestor(root->right,p,q);

        if(!left) return right;
        if(!right) return left;
        return root;
    }
};

2-Binary tree traversal (focus on mastering non-recursive algorithms)

144. Preorder traversal of binary tree

Algorithm 1: Recursive writing

/**
 * 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) return;
        res.push_back(root->val);
        dfs(root->left,res);
        dfs(root->right,res);
    }
};

Algorithm 2: Non-recursive 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> preorderTraversal(TreeNode* root) {
    
    
        vector<int> res;
        stack<TreeNode*> S;

        while(root || !S.empty()) // 模板写法
        {
    
    
            while(root){
    
    
                res.push_back(root->val);
                S.push(root);
                root = root->left;
            }
            if(!S.empty())
            {
    
    
                root = S.top();
                S.pop();
                root = root->right;
            }
        }

        return res;
    }
};

94. In-order traversal of binary tree

Algorithm 1: Recursive writing

/**
 * 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> res;
        dfs(root,res);
        return res;
    }

    void dfs(TreeNode* root,vector<int> &res)
    {
    
    
        if(!root) return;
        dfs(root->left,res);
        res.push_back(root->val);
        dfs(root->right,res);
    }
    
};

Algorithm 2: Non-recursive traversal

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

        return res;
    }
};

145. Post-order traversal of binary tree

Algorithm 1: Recursive writing

/**
 * 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> res;
        dfs(res,root);
        return res;
    }

    void dfs(vector<int> &res,TreeNode* root) // 各种遍历只是递归顺序不同
    {
    
    
        if(!root) return; // 记得递归返回
        if(root->left) dfs(res,root->left);
        if(root->right) dfs(res,root->right);
        res.push_back(root->val);
    }
};

Algorithm 2: Non-recursive traversal

The order of the post-order traversal is: left and right roots, and the order of the first-order traversal is: the root left and right; the
pre-order traversal can be modified as: the root is right and left, and then transposed; that is, the left and right roots are obtained, and the post-order traversal is obtained.

/**
 * 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> res;
        stack<TreeNode*> S;

        while(root || !S.empty()) // 模板写法
        {
    
    
            while(root){
    
    
                res.push_back(root->val);
                S.push(root);
                root = root->right;
            }

            if(!S.empty())
            {
    
    
                root = S.top();
                S.pop();
                root = root->left;
            }
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

Strictly follow the definition, output when the third visit

/**
 * 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:
    vector<int> postorderTraversal(TreeNode* root) {
    
    
        vector<int> res;
        stack<TreeNode*> S;

        TreeNode *cur = root, *pre = NULL;
        while(cur || !S.empty())
        {
    
    
            while(cur){
    
    
                S.push(cur);
                cur = cur -> left;
            }

            cur = S.top();
            if(cur->right == NULL || cur->right == pre){
    
    
                res.push_back(cur->val);
                S.pop();
                pre = cur;
                cur = NULL;
            }else{
    
    
                cur = cur->right;
            }
        }

        return res;
    }
};

102. Sequence traversal of binary tree

/**
 * 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<vector<int>> levelOrder(TreeNode* root) {
    
    
        vector<vector<int>> res;
        if(!root) return res; 
        queue<TreeNode*> q;
        q.push(root);

        while(q.size())
        {
    
    
            int n = q.size();
            vector<int> cur;
            while(n--)
            {
    
    
                auto t = q.front();
                q.pop();
                cur.push_back(t->val);
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            res.push_back(cur);
        }

        return res;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43154149/article/details/108718579