LintCode-596: Minimum Subtree

LLaMA Factory多卡微调的实战教程(持续更新) 本文主要介绍了LLaMA Factory多卡微调的实战教程(持续更新),希望对学习大语言模型的同学们有所帮助。 文章目录 1. 前言 2. 配置环境 2.1 安装虚拟环境 2.2 安装依赖库 2.3 模型选型与下载 2.4 配置多卡环境 3. 运行代码 3.1 构建训练数据 3.2 修改配置文件 3.3 微调 3.3.1 zero3微调 3.3.2 启动API 3.3.3 模型测试 阅读详情

这道题我感觉并不是很简单,因为递归的同时还要考虑SUM,但是又只能返回TreeNode *。
我用的递归,并假定NULL节点的SUM为0。用的全局变量来保存minValue和RecordNode。
这题要注意的地方:是当某个节点的左子节点或右子节点是NULL时,不能直接设minChildSum=min(sumLeft, sumRight); 因为NULL节点的SUM为0,所以肯定最小。测试特例:input = {1,#,2}。

代码如下:

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: the root of the minimum subtree
     */
    TreeNode * findSubtree(TreeNode * root) {
        if (!root) return NULL;
        int sumLeft = sumSubTree(root->left);
        int sumRight = sumSubTree(root->right);
        int sumAll = sumLeft + sumRight + root->val;
        int minChildSum;
        if (!root->left) 
            minChildSum = sumRight;
        else if (!root->right) 
            minChildSum = sumLeft;
        else 
            minChildSum = min(sumLeft, sumRight);
        
        if (sumAll < minValue) {
            minValue = sumAll;
            recordNode = root;
        } else if (minChildSum < minValue) {
            minValue = minChildSum;
            recordNode = (sumLeft< sumRight)? root->left : root->right; 
        }
   
        return recordNode;
    }

private:
    int sumSubTree(TreeNode * root) {
        if (!root) 
            return 0;
        else {
            int sum = sumSubTree(root->left) + sumSubTree(root->right) + root->val; 
            if (sum < minValue) {
                minValue = sum;
                recordNode = root;
            }
            return sum;  
        }
    }

    int minValue=INT_MAX;
    TreeNode *recordNode;
};

另外,我看了一下九章的方法是将返回类型包装成一个结构,包括TreeNode*, sum和minSum。这个技巧值得学习。
其参考代码如下,需要好好掌握。

// version 2: Pure divide conquer
class ResultType {
    public TreeNode minSubtree;
    public int sum, minSum;
    public ResultType(TreeNode minSubtree, int minSum, int sum) {
        this.minSubtree = minSubtree;
        this.minSum = minSum;
        this.sum = sum;
    }
}

public class Solution {
    /**
     * @param root the root of binary tree
     * @return the root of the minimum subtree
     */
    public TreeNode findSubtree(TreeNode root) {
        ResultType result = helper(root);
        return result.minSubtree;
    }
    
    public ResultType helper(TreeNode node) {
        if (node == null) {
            return new ResultType(null, Integer.MAX_VALUE, 0);
        }
        
        ResultType leftResult = helper(node.left);
        ResultType rightResult = helper(node.right);
        
        ResultType result = new ResultType(
            node,
            leftResult.sum + rightResult.sum + node.val,
            leftResult.sum + rightResult.sum + node.val
        );
        
        if (leftResult.minSum <= result.minSum) {
            result.minSum = leftResult.minSum;
            result.minSubtree = leftResult.minSubtree;
        }
        
        if (rightResult.minSum <= result.minSum) {
            result.minSum = rightResult.minSum;
            result.minSubtree = rightResult.minSubtree;
        }
        
        return result;
    }
}

另外,发现还可以用超简洁的分治+遍历来解决这个问题(参考九章),代码如下:

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: the root of the minimum subtree
     */
    TreeNode * findSubtree(TreeNode * root) {
        helper(root);
        return recordRoot;
    }

private:
    int helper(TreeNode * root) {
        if (!root) 
            return 0;
            
        int sum = helper(root->left)+helper(root->right)+root->val;
        if (sum < minSum) {
            recordRoot = root;
            minSum = sum;
        }
        return sum;
    }

    int minValue=INT_MAX;
    TreeNode *recordNode;
};

三刷:

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: the root of the minimum subtree
     */
    TreeNode* findSubtree(TreeNode *root) {
        helper(root);
        return minNode;
    }
    
private:
    int helper(TreeNode *root) {
        if (!root) return 0;
        if (!root->left && !root->right) {
            if (root->val < minSum) {
                minSum = root->val;
                minNode = root;
            }
            return root->val;
        }
        int resLeft = helper(root->left);
        int resRight = helper(root->right);
        int resSelf = resLeft + resRight + root->val;
        if (resLeft && resLeft < resRight && resLeft < resSelf && resLeft < minSum) {
            minSum = resLeft;
            minNode = root->left;
        } else if (resRight && resRight < resLeft && resRight < resSelf && resRight < minSum) {
            minSum = resRight;
            minNode = root->right;
        } else if (resSelf < minSum) {
            minSum = resSelf;
            minNode = root;
        }
        return resSelf;
    }
    int minSum = INT_MAX;
    TreeNode *minNode = nullptr;
};
YOLOv11改进策略【Neck】| 替换RT-DETR中的CCFF跨尺度特征融合颈部结构,优化计算瓶颈与冗余问题 RT-DETR中的CCFF模块用于优化多尺度特征融合,解决传统方法的计算瓶颈与冗余问题,提升模型速度与精度,以适应实时检测需求。 阅读详情

相关推荐

Protege本体建模实战:从零构建电影知识图谱并导入Neo4j

本体建模是知识图谱构建的基石,通过类、属性、实例和约束系统描述领域知识,为数据提供可复用的语义骨架。OWL作为本体描述语言的标准,配合Protege工具可实现可视化建模,并借助推理机验证逻辑一致性,提升模型可靠性。在工程落地中,本体模型常需导入图数据库如Neo4j,以支持高效的关联查询与业务应用。本文以影视领域为背景,完整演示Protege中定义类层级、创建对象/数据属性、填充实例,以及导出OWL文件并通过Python脚本转换为Neo4j节点与关系的全过程,同时梳理命名空间、匿名节点、编码等易踩坑点,帮助读

weixin_29047545的博客 279

21 结点之和最小的子树(Minimum Subtree)

文章目录1 题目2 解决方案2.1 思路2.2 时间复杂度2.3 空间复杂度3 源码3.1 遍历法 1 题目 题目:结点之和最小的子树(Minimum Subtree) 描述:给一棵二叉树, 找到和为最小的子树, 返回其根节点。输入输出数据范围都在int内。 lintcode题号——596,难度——easy 样例1: 输入:{1,-5,2,1,2,-4,-5} 输出:1 解释: 这棵树如下所示: 1 / \ -5 2 / \ / \ 1 2 -4 -5 整颗

SeeDoubleU的专栏 639

Fortify SCA 24.4 最新可用版

Fortify Static Code Analyzer(Fortify SCA) 是一款由 Micro Focus(原惠普旗下)开发的 静态应用安全测试(SAST)工具,专注于在软件开发早期通过分析源代码或编译后的二进制文件,识别潜在的安全漏洞、代码缺陷和合规性问题。以下是其核心特点与功能:核心功能 静态代码分析 在不运行程序的情况下,通过语法、语义分析检测代码中的安全漏洞(如 SQL 注入、跨站脚本(XSS)、缓冲区溢出等)和代码质量问题。

棉花糖的博客 3180

LintCode 596 Minimum Subtree

思路 裸分治法。使用ResultType作为返回类型可以避免使用全局变量。 复杂度 时间复杂度O(n) 空间复杂度O(logn) 代码 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public T...

我要上岸的博客 289

基于FLUENT的圆柱绕流数值模拟与流场优化策略

本文详细介绍了基于FLUENT的圆柱绕流数值模拟方法与流场优化策略。从基础概念、建模技巧到湍流模型选择,全面解析了圆柱绕流模拟的关键技术,并提供了实用的网格划分和参数设置建议。通过具体案例展示了流动控制方法和优化流程,帮助工程师有效降低阻力系数,提升流场性能。

f6g7h8i9j的博客 703

土壤水动力学_前 言

Van Genuchten (V-G) 模型是土壤水分特征曲线中最常用的模型之一。它通过数学方程描述了土壤基质吸力与体积含水量之间的关系,被广泛应用于土壤水分运移、土壤盐渍化、边坡稳定性等研究领域。V-G模型是描述土壤水分特征曲线的重要工具,但它并不是完美的。在实际应用中,需要根据土壤类型、试验条件等因素选择合适的模型。土壤基质吸力是土壤水分学中的一个重要概念,它对土壤水分的保持、植物的生长以及土壤的工程性质都有重要影响。

weixin_44259522的博客 5324

【一次过】Lintcode 596. 最小子树

给一棵二叉树, 找到和为最小的子树, 返回其根节点。 样例 例1: 输入: 1 / \ -5 2 / \ / \ 0 2 -4 -5 输出:1 例2: 输入: 1 输出:1 注意事项 LintCode会打印根节点为你返回节点的子树。保证只有一棵和最小的子树并且给出的二叉树不是一棵空树。 解题思路1: 采用全局变量记录最...

majichen95的博客 549

嵌入式SDIO 总线面试题及参考答案

SDIO 的 4 位总线模式是一种提高数据传输效率的工作模式。在传统的 1 位总线模式下,数据只能通过一根数据线(DAT0)进行传输,这限制了数据传输的带宽和速度。而在 4 位总线模式下,DAT0 - DAT3 这 4 根数据线同时用于数据传输,相当于在同一时间内可以传输 4 倍于 1 位模式的数据量,从而显著提高了数据传输的带宽和速度。要通过 CMD6 命令将 SDIO 总线从当前模式切换到 4 位总线模式,需要遵循以下步骤:第一步,主设备需要确保 SDIO 卡支持 4 位总线模式。

大模型大数据攻城狮的专栏 1715

Minimum Subtree

我的错误代码 理递归思路 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = va...

weixin_34265814的博客 132

2026最新版OpenClaw安装:Windows WSL2从零到跑通

Windows WSL2安装OpenClaw指南 本文详细介绍了在Windows系统上通过WSL2部署OpenClaw AI助理的完整流程。WSL2结合了Windows易用性和Linux高性能,特别适合Windows用户、开发者、学生和技术爱好者。文章涵盖硬件要求(推荐i5/16GB/SSD)、系统准备(Windows 10/11+WSL2)、环境部署(Python/Node.js/Redis)和OpenClaw配置(克隆/编译/初始化)等关键步骤。通过WSL2,用户可在保留Windows操作习惯的同

一键难忘的博客 2345

Unity技术开发干货 - 总目录

总目录 欢迎大家来到 passion 的《Unity技术开发干货》开始前博主先列出学习的大纲,同时也供大家学习参考,如果有什么问题,欢迎留言。 一.Unity基础: Unity基础-脚本生命周期 Unity基础-向量 Unity基础-寻路系统 Unity基础-物理系统 Unity基础-Mecanim动画系统 Unity基础-UGUI Unity基础-灯光 Unity-UGUI之Event System Unity-2018-粒子系统 Unity-协程入门 Unity-2D物理引擎、铰链、效应器 Unity-

TxNet 4256

Lintcode 596. Minimum Subtree

题目 Given a binary tree, find the subtree with minimum sum. Return the root of the subtree. 我的想法 结合分治和遍历。利用遍历中的全局变量来记录最小值,利用分治的向上汇报来计算每一个结点的数值和 class Result { TreeNode root; int sum; Result...

努力学习的姜饼 285

ABAP CDS View 从入门到精通教程第一篇:ABAP CDS View 在 SAP 技术体系中的重要地位

摘要:本文介绍了SAP开发技术专家提供的多套SAP开发教程,涵盖ABAP、UI5、Fiori、OData、BTP等技术领域。重点阐述了ABAP CDS View在SAP S/4HANA中的核心地位,作为虚拟数据模型(VDM)的关键技术,它实现了OLTP和OLAP的统一处理。文章还说明了学习CDS View的前置条件,包括需要SAP NetWeaver 7.50 SP01或更高版本系统,以及使用SAP ABAP Development Tool进行开发实践。作者具有16年SAP开发经验,这些教程形成了完整的S

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 896

lintcode596:Minimum Subtree

问题描述 https://www.lintcode.com/problem/minimum-subtree/description java实现 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * pu...

zero 226

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【Lintcode】596. Minimum Subtree

题目地址: https://www.lintcode.com/problem/minimum-subtree/description 给定一个二叉树,求其和最小子树的树根。 最简便的方法是开一个全局变量,然后用递归的方式计算所有子树的和,同时不停更新那个最小子树和与那个树根。这样的好处是,回避了函数传递变量的麻烦。主要的原因是,我们在递归计算子树和的时候,不但要知道左右子树分别的最小子树和与两个最...

数学、算法爱好者的博客 245

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[LintCode] 596 Minimum Subtree 解题报告

DescriptionGiven a binary tree, find the subtree with minimum sum. Return the root of the subtree.NoticeLintCode will print the subtree which root is your return node.It's guaranteed that there is onl...

weixin_30292745的博客 165

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

lintcode596- Minimum Subtree- easy

Given a binary tree, find the subtree with minimum sum. Return the root of the subtree. Notice LintCode will print the subtree which root is your return node.It's guaranteed that there is only o...

weixin_30651273的博客 157

LintCode Minimum Subtree java solution

description Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.NoticeLintCode will print the subtree which root is your return node. It’s guaranteed that there is

ncst 1246

Minimum Subtree 二叉树分治法例题

给一个二叉树,要找和尽量小的subtree。 private TreeNode subTree = null; //用于记载最小的subtree的根结点 private int subTreeSum = Integer.MAX_VALUE; public TreeNode findSubTree (TreeNode root){ subTreeSum = helper(root); retu...

keke今天又学算法了 247

[LeetCode] Smallest Subtree with all the Deepest Nodes 包含最深结点的最小子树

Given a binary tree rooted atroot, thedepthof each node is the shortest distance to the root. A node isdeepestif it has the largest depth possible amongany node in theentire tree. The subtree ...

weixin_30432007的博客 158

【LeetCode】1403. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法贪心日期 题目地址:https://leetcode-cn.com/problems/minimum-subsequence-in-non-increasing-order/ 题目描述 给你一个数组 nums,请你从中抽取一个子序列,满足该...

负雪明烛 1263

LeetCode刷题笔记(贪心):minimum-window-substring

转载请注明作者和出处:http://blog.youkuaiyun.com/u011475210 代码地址:https://github.com/WordZzzz/Note/tree/master/LeetCode 刷题平台:https://www.nowcoder.com/ta/leetcode 题  库:Leetcode经典编程题 编  者:WordZzzz 题目描述 解题思路 C版代码实现

WordZzzz 1243

Codeforces 893F Subtree Minimum Query(Hard) 主席树

题目大意:给定一棵有根树,点x有点权a[x],多组询问,每次询问以x为根的子树中的所有满足dep[y]-dep[xi]<=ki的y中,最小的a[y]。n<=1e5, q<=1e6。强制在线。 题解:按照dfs序重新编号,这个题等价于求编号在[L[x],R[x]],深度在[dep[x],dep[x]+k]中的点权最小值。 因此就是一个二维数点(权最值)。但是随意一个矩形的最值类询问还要树套树什么的

Mys_C_K的博客 668

LintCode - Binary Tree & Divide and Conquer

Traverse the Binary Tree preorder  (visit, left, right) Binary Tree Preorder Traversal [easy] inorder     (left, visit, right) Binary Tree Inorder Traversal [easy] postorder(left, right, visit) ...

real_lisa的博客 273

[LeetCode 866] Smallest Subtree with all the Deepest Nodes

题目 给定一个根为 root 的二叉树,每个结点的深度是它到根的最短距离。 如果一个结点在整个树的任意结点之间具有最大的深度,则该结点是最深的。 一个结点的子树是该结点加上它的所有后代的集合。 返回能满足“以该结点为根的子树中包含所有最深的结点”这一条件的具有最大深度的结点。 示例: 输入:[3,5,1,6,2,0,8,null,null,7,4] 输出:[2,7,4] 解释: ...

wlhwaii的博客 479
上一篇: 堆Heap和优先队列(Priority Queue)学习小结
下一篇: LintCode-480: Binary Tree Paths
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值