letcode 分类练习 513.找树左下角的值 112. 路径总和 106.从中序与后序遍历序列构造二叉树

letcode 分类练习 513.找树左下角的值 112. 路径总和 106.从中序与后序遍历序列构造二叉树

513.找树左下角的值

遍历二叉树,并记录当前的深度,如果深度大于最大深度,那么更新该节点,为了保证是最左的,我们规定顺序一定是先遍历左孩子再遍历右孩子

c 复制代码
class Solution {
public:
    int depth = 0;
    int result;
    void getV(TreeNode* root, int d){
        if(!root) return;
        if(!root->left && !root -> right){
            if(d > depth){depth = d; result = root -> val;}
        }
        getV(root -> left, d+1);
        getV(root -> right, d+1);
    }
    int findBottomLeftValue(TreeNode* root) {
        getV(root, 1);
        return result;

    }
};

112. 路径总和

还是对树进行一个dfs遍历,如果当前遍历的节点是叶子节点,就判断当前累计的sum是不是target

c 复制代码
class Solution {
public:
    bool find = false;
    void dfs(TreeNode* root, int sum,int targetSum){
        if(!root) return;
        if(sum  == targetSum && !root->left && !root->right){find = true; return;}
        
        if(root->left)dfs(root->left, sum + root -> left -> val, targetSum);
        if(root->right)dfs(root->right, sum + root -> right -> val, targetSum);
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root)return false;
        dfs(root, root -> val, targetSum);
        return find;
    }
};

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

这道题有意思,我们做题的时候构造二叉树都是先找后序序列的最后一个元素是根,然后根据这个根切割中序序列,然后再找中序序列前后两个分割序列,注意我们得到了中序分割的左右子树序列,那么我们可以这个左右子树序列的长度再对后序数组进行一个分割:

积累一个vector查询的方式:

c 复制代码
// 这个查询是[)区间左闭右开
auto y = find(inorder.begin()+in_left, inorder.begin() + in_right + 1, val);
int index = distance(inorder.begin(), y);

总代码如下:

c 复制代码
class Solution {
public:
    TreeNode* build(vector<int>& inorder, vector<int>& postorder, int in_left, int in_right, int po_left,int po_right){
        if(in_left > in_right || po_left > po_right) return nullptr;
        int val = postorder[po_right];
        auto y = find(inorder.begin()+in_left, inorder.begin() + in_right + 1, val);
        int index = distance(inorder.begin(), y);
        int left_length = index - in_left;
        int right_length = in_right - index;
        TreeNode* n = new TreeNode(postorder[po_right]);
        n -> left = build(inorder, postorder, in_left, in_left + left_length -1, po_left, po_left + left_length -1);
        n->right = build(inorder, postorder, in_right - right_length + 1 , in_right, po_right - 1 - right_length + 1, po_right - 1);
        return n;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size() == 0) return nullptr;
        return build(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
    }
};
相关推荐
MMjeaty13 分钟前
数据结构——栈和队列
数据结构·算法
机器学习之心5 小时前
多目标鲸鱼优化算法(NSWOA),含46种测试函数和9个评价指标,MATLAB实现
算法·matlab·多目标鲸鱼优化算法·46种测试函数·9个评价指标
max5006006 小时前
基于Meta Llama的二语习得学习者行为预测计算模型
人工智能·算法·机器学习·分类·数据挖掘·llama
王哥儿聊AI7 小时前
Lynx:新一代个性化视频生成模型,单图即可生成视频,重新定义身份一致性与视觉质量
人工智能·算法·安全·机器学习·音视频·软件工程
手握风云-9 小时前
优选算法的寻踪契合:字符串专题
算法
闭着眼睛学算法9 小时前
【华为OD机考正在更新】2025年双机位A卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】
java·c语言·javascript·c++·python·算法·华为od
IT古董9 小时前
【第五章:计算机视觉-项目实战之目标检测实战】2.目标检测实战:中国交通标志检测-(2)中国交通标志检测数据格式转化与读取
算法·目标检测·计算机视觉
MobotStone9 小时前
LLM 采样入门到进阶:理解与实践 Top-K、Top-P、温度控制
算法
杨小码不BUG10 小时前
CSP-J/S初赛知识点精讲-图论
c++·算法·图论··编码·csp-j/s初赛
LeaderSheepH11 小时前
常见的排序算法
数据结构·算法·排序算法