二叉树相关操作

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);
        }
    }
};

非递归:
依然采用先将左节点放入栈中,获取结果的时候分为两种情况:

  • 当前节点有右节点且没有访问过,先去访问右节点,并用 pre 标记访问过的右节点;
  • 没有右节点或者或者右节点已经访问过,当前节点的值可以放入结果数组。
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        TreeNode *pre = nullptr;
        while (root || !st.empty()) {
            while (root) {
                st.push(root);
                root = root->left;
            }

            if (!st.empty()) {
                auto cur = st.top();
                if (!cur->right || cur->right == pre) {
                    st.pop();
                    res.push_back(cur->val);
                    pre = cur;
                    root = nullptr;
                } else {
                    root = cur->right;
                }
            }
        }
        return res;
    }
};

反向逻辑,将右节点放入栈中,将结果逆序存储

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        TreeNode *pre = nullptr;
        while (root || !st.empty()) {
            while (root) {
                st.push(root);
                root = root->left;
            }

            if (!st.empty()) {
                auto cur = st.top();
                if (!cur->right || cur->right == pre) {
                    st.pop();
                    res.push_back(cur->val);
                    pre = cur;
                    root = nullptr;
                } else {
                    root = cur->right;
                }
            }
        }
        return res;
    }
};

最近公共祖先
递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int find(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q, struct TreeNode** ans){
    if (root == NULL) {
        return 0;
    }
    int cur = root == p || root == q;
    int l = find(root->left, p, q, ans);
    int r = find(root->right, p, q, ans);
    if (cur + l + r >= 2) {
        *ans = root;
    }
    return cur || l || r;
}

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q){
    struct TreeNode* ans = NULL;
    find(root, p, q, &ans);
    return ans;
}

非递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int getF(struct TreeNode* root, struct TreeNode *s, struct TreeNode **path) {
    int len = 0;
    struct TreeNode* stack[10005], *pre = NULL;
    int top = -1;
    
    while (root || top >= 0) {
        while (root) {
            stack[++top] = root;
                        //printf("root %d\n", root->val);
            path[len++] = root;
            if (root == s) {
                return len;
            }
            root = root->left;
        }

        if (top >= 0) {
            root = stack[top];
            if (!root->right || root->right == pre) {
                --top;
                --len;
                pre = root;
                root = NULL;
            } else {
                root = root->right;
            }
        }
    }
    return len;
}

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q){
    struct TreeNode* path1[10005], *path2[10005];
    int len1, len2;
    len1 = getF(root, p, path1);
    len2 = getF(root, q, path2);
    if (len1 < len2) {
        len2 = len1;
    }
    struct TreeNode *res = NULL;
    for (int i = 0; i < len2; i++) {
        //printf("%d  %d\n", path1[i]->val, path2[i]->val);
        if (path1[i] != path2[i]) {
            return res;
        }
        res = path1[i];
    }
    return res;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值