LeetCode二叉树的题目

本文介绍了四种针对二叉树的操作算法,包括使用递归和BFS找树的左下角值,递归解决路径总和问题,以及通过前序和中序/后序遍历序列构造二叉树的方法,所有方法的时间复杂度均为O(n),空间复杂度与树的高度有关。
摘要由CSDN通过智能技术生成

513. 找树左下角的值

方法:递归

/**
 * 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 {
private:
    int maxv = INT_MIN;
    int val = 0;
    void solve(TreeNode* cur, int dep) {
        if (!cur->left && !cur->right) {
            if (maxv < dep) {
                maxv = dep;
                val = cur->val;
            }
            return ;
        }

        if (cur->left) solve(cur->left, dep+1);
        if (cur->right) solve(cur->right, dep+1);

    }
public:
    int findBottomLeftValue(TreeNode* root) {
        solve(root, 1);
        return val;
    }
};

$时间复杂度O(n),空间复杂度O(h)h为树的高度

方法:bfs

/**
 * 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:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        int val;
        while (!q.empty()) {
            int n = q.size();
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (!i) val = nod->val;
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right); 
            }
        }
        return val;
    }
};

$时间复杂度O(n),空间复杂度O(h)h为树的高度

112. 路径总和

方法:递归

/**
 * 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 {
private:
    bool solve(TreeNode* cur, int mut) {
        if (!cur->left && !cur->right && !mut) return true;
        if (!cur->left && !cur->right) return false;

        if (cur->left) if (solve(cur->left, mut-cur->left->val)) return true;
        if (cur->right) if (solve(cur->right, mut-cur->right->val)) return true;
        return false;
    }
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == NULL) return false;
        return solve(root, targetSum-root->val);
    }
};

$时间复杂度O(n),空间复杂度O(h)h为树的高度

113. 路径总和 II

方法:递归

/**
 * 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 {
private:
    vector<vector<int>> res;
    vector<int> path;
    void solve(TreeNode* cur, int mut) {
        if (!cur->left && !cur->right && !mut) {
            res.push_back(path);
        }

        if (cur->left) {
            path.push_back(cur->left->val);
            solve(cur->left, mut-cur->left->val);
            path.pop_back();
        }

        if (cur->right) {
            path.push_back(cur->right->val);
            solve(cur->right, mut-cur->right->val);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        res.clear();
        path.clear();
        if (root == NULL) return res;
        path.push_back(root->val);
        solve(root, targetSum-root->val);
        return res;
    }
};

$时间复杂度O(n^{2}),空间复杂度O(n)

105. 从前序与中序遍历序列构造二叉树

方法:分治+递归

/**
 * 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 {
private:
    TreeNode* construct_binary_tree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe) {
        if (pb == pe) return NULL;
        int val = postorder[pe-1];
        TreeNode* root = new TreeNode(val);
        if (pe - pb == 1) return root;
        int k;

        for (k = 0; k < ie; ++k) {
            if (inorder[k] == val) break;
        }
        
        int lib = ib, lie = k, rib = k + 1, rie = ie;
        int lpb = pb, lpe = k + pb - ib, rpb = k + pb - ib, rpe = pe - 1;

        root->left = construct_binary_tree(inorder, lib, lie, postorder, lpb, lpe);
        root->right = construct_binary_tree(inorder, rib, rie, postorder, rpb, rpe);  
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return construct_binary_tree(inorder, 0, inorder.size(), postorder, 0, postorder.size());
    }
};

$时间复杂度O(n^{2}),空间复杂度O(h)h为树的高度

106. 从中序与后序遍历序列构造二叉树

方法:分治+递归

/**
 * 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 {
private:
    TreeNode* construct_binary_tree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe) {
        if (pb == pe) return NULL;
        int val = postorder[pe-1];
        TreeNode* root = new TreeNode(val);
        if (pe - pb == 1) return root;
        int k;

        for (k = 0; k < ie; ++k) {
            if (inorder[k] == val) break;
        }
        
        int lib = ib, lie = k, rib = k + 1, rie = ie;
        int lpb = pb, lpe = k + pb - ib, rpb = k + pb - ib, rpe = pe - 1;

        root->left = construct_binary_tree(inorder, lib, lie, postorder, lpb, lpe);
        root->right = construct_binary_tree(inorder, rib, rie, postorder, rpb, rpe);  
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return construct_binary_tree(inorder, 0, inorder.size(), postorder, 0, postorder.size());
    }
};

$时间复杂度O(n^{2}),空间复杂度O(h)h为树的高度

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值