重拾C++之菜鸟刷算法第9篇---二叉树(中2)

文章介绍了四个与二叉树相关的LeetCode题目:1.计算左叶子之和,2.寻找左下角值,3.判断路径和,以及4.通过中序和后序遍历重建二叉树。涉及递归和迭代方法解决这些问题。
摘要由CSDN通过智能技术生成

十一、左叶子之和

题目

给定二叉树的根节点 root ,返回所有左叶子之和。

404. 左叶子之和 - 力扣(LeetCode)

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        // 递归停止条件
        if(root == nullptr) return 0;
        if(root->left == nullptr && root->right == nullptr) return 0;

        // 单层迭代条件,当遇到符合条件则赋值  左
        int left = sumOfLeftLeaves(root->left);
        if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr){
            left = root->left->val;
        }
        // 右
        int right = sumOfLeftLeaves(root->right);
        // 中
        return right + left;
    }
};

十二、找树左下角的值

题目

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

513. 找树左下角的值 - 力扣(LeetCode)

题解 – 层次遍历

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        int result;
        if(root == nullptr) return result;
        q.push(root);
        while(!q.empty()){
            int size = q.size();
            vector<int> vec;
            while(size--){
                TreeNode* tmp = q.front();
                q.pop();
                vec.push_back(tmp->val);
                if(tmp->left) q.push(tmp->left);
                if(tmp->right) q.push(tmp->right);
            }
            result = vec[0];
        }
        
        return result;
    }
};

题解 – 递归

class Solution {
public:
    int maxDepth = INT_MIN;
    int result;
    void traverse(TreeNode* root, int depth){
        if(root->left == nullptr && root->right == nullptr){
            if(depth > maxDepth){
                maxDepth = depth;
                result = root->val;
            }
            return;
        }

        if(root->left){
            depth++;
            traverse(root->left, depth);
            depth--;
        }

        if(root->right){
            depth++;
            traverse(root->right, depth);
            depth--;
        }
    }
    int findBottomLeftValue(TreeNode* root) {
       traverse(root, 0);
       return result;
    }
};

十三、路径总和

题目

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false

叶子节点 是指没有子节点的节点

112. 路径总和 - 力扣(LeetCode)

题解

class Solution {
public:
    bool traverse(TreeNode* cur, int count){
        if(!cur->left && !cur->right && count == 0) return true;
        // 没有找到合适的边
        if(!cur->left && !cur->right) return false;

        if(cur->left){
            count -= cur->left->val;
            if(traverse(cur->left, count)) return true;
            count += cur->left->val;
        }

        if(cur->right){
            count -= cur->right->val;
            if(traverse(cur->right, count)) return true;;
            count += cur->right->val;
        }
        
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr) return false;
        return traverse(root, targetSum - root->val);
    }
};

十四、从中序与后序遍历序列构造二叉树

一定要主要区间统一性~

  • 后序数组最后一个元素为中
  • 切割中序数组
  • 分成左右中序数组
  • 后序数组resize
  • 分成左右后序数组
  • traverse

题目

给定两个整数数组 inorderpostorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树

106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

class Solution {
public:
    TreeNode* traverse(vector<int>& inorder, vector<int>& postorder){
        if(postorder.size() == 0) return nullptr;
        // 拿到后序遍历的最后一个元素
        TreeNode* root = new TreeNode(postorder[postorder.size() - 1]);
        if(postorder.size() == 1) return root;
        // 找到中序遍历的切割点
        int divideIndex = 0;
        for(; divideIndex < inorder.size(); divideIndex++){
            if(inorder[divideIndex] == postorder[postorder.size() - 1]) break;
        }
        // 左闭右开
        vector<int> leftInorder(inorder.begin(), inorder.begin() + divideIndex);
        vector<int> rightInorder(inorder.begin() + divideIndex + 1, inorder.end());

        // 后序
        postorder.resize(postorder.size() - 1);

        vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
        vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());

        root->left = traverse(leftInorder, leftPostorder);
        root->right = traverse(rightInorder, rightPostorder);
        
        return root;

    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size() == 0 || postorder.size() == 0) return nullptr;
        return traverse(inorder, postorder);
    }
};
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值