代码随想录算法训练营第十七、十八天 | LC 110. 平衡二叉树、257. 二叉树的所有路径、404. 左叶子之和、 513. 找树左下角的值、 112. 路径总和、106. 从中序与后序遍历序列构

110. 平衡二叉树

代码

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (!root) return true;
        int left = isHigh(root->left);
        int right = isHigh(root->right);
        if (abs(left - right) > 1) return false;
        return isBalanced(root->left) && isBalanced(root->right);
    }
    int isHigh(TreeNode* root){
        if (!root) return 0;
        return max(isHigh(root->left), isHigh(root->right)) + 1;
    }
};

257. 二叉树的所有路径

代码

class Solution {
public:
    vector<string> res;
    vector<int> path;
    vector<string> binaryTreePaths(TreeNode* root) {
        if (!root) return res;
        traversal(root);
        return res;
    }
    void traversal(TreeNode* root){
        path.push_back(root->val);
        if (root->left == nullptr && root->right == nullptr){
            string s = "";
            for (int i = 0; i < path.size() - 1;i ++){
                s += to_string(path[i]);
                s += "->";
            }
            s += to_string(path[path.size() - 1]);
            res.push_back(s);
            return;
        }
        if (root->left){
            traversal(root->left);
            path.pop_back();
        }
        if (root->right){
            traversal(root->right);
            path.pop_back();
        }
    }
};

404. 左叶子之和

代码

class Solution {
public:
    int res =  0;
    int sumOfLeftLeaves(TreeNode* root) {
        if (!root) return 0;
        if (root->left && root->left->left == nullptr && root->left->right == nullptr){
            res += root->left->val;
        }
        sumOfLeftLeaves(root->left);
        sumOfLeftLeaves(root->right);
        return res;
    }
};

513. 找树左下角的值

代码

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode*> q;
        int res;
        q.push(root);

        while(!q.empty()){
            int size = q.size();
            res = q.front()->val;

            for (int i = 0; i < size; i ++){
                auto a = q.front();
                q.pop();
                if (a->left) q.push(a->left);
                if (a->right) q.push(a->right);
            }
        }
        return res;
    }
};

112. 路径总和

代码

class Solution {
public:
    bool res = false;
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return res;;
        if (root->val == targetSum && root->left == nullptr && root->right == nullptr){
            res = true;
            return res;
        };
        hasPathSum(root->left, targetSum - root->val);
        hasPathSum(root->right, targetSum - root->val);
        return res;
    }
};

113. 路径总和 II

代码

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if (!root) return {};
        path.push_back(root->val);
        if (root->val == targetSum && root->left == nullptr && root->right == nullptr){
            res.push_back(path);
        }
        if (root->left){
            pathSum(root->left, targetSum - root->val);
            path.pop_back();
        }
        if (root->right){
            pathSum(root->right, targetSum - root->val);
            path.pop_back();
        }
        return res;
    }
};

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

代码

class Solution {
public:
    unordered_map<int, int> map;
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int n = inorder.size();
        for (int i = 0; i < n; i ++){
            map[inorder[i]] = i;
        }
        return dfs(inorder, postorder, 0, n - 1, 0 , n -1);
    }

    TreeNode* dfs(vector<int>& inorder, vector<int>& postorder, int il, int ir, int pl, int pr){
        if (pl > pr) return nullptr;
        int k = map[postorder[pr]] - il;
        TreeNode* root = new TreeNode(postorder[pr]);
        root->left = dfs(inorder, postorder, il, il + k - 1, pl, pl + k - 1);
        root->right = dfs(inorder, postorder, il + k + 1, ir, pl + k, pr - 1);
        return root;
    }
};

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

代码

class Solution {
public:
    unordered_map<int, int> pos;
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int n = inorder.size();
        for (int i = 0; i < n; i ++){
            pos[inorder[i]] = i;
        }
        return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
    }
    TreeNode* dfs(vector<int>& pre, vector<int>& ino, int pl, int pr, int ll, int lr){
        if (pl > pr) return NULL;
        int k = pos[pre[pl]] - ll;
        TreeNode* root = new TreeNode(pre[pl]);
        root->left = dfs(pre, ino, pl + 1, pl + k, ll, ll + k - 1);
        root->right = dfs(pre, ino, pl + k + 1, pr, ll + k + 1, lr);
        return root;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值