[LeetCode]563. Binary Tree Tilt

LeetCode --- 563. Binary Tree Tilt 解题报告 Given a binary tree, return the tilt of thewhole tree. The tilt of atree nodeis defined as theabsolute differencebetween the sum of all left subtree node values and the sum of all right subtree ... 阅读详情

[LeetCode]563. Binary Tree Tilt

题目描述

这里写图片描述

思路

深搜
因为是要计算左右子树节点和之间的绝对值,所以递归的返回值是子树节点的和(即需要拆两个函数)。
在递归的同时计算结果,注意因为是递归调用函数,因此需要临时变量来保存调用结果,不可以多次调用

代码

#include <iostream>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    int findTilt(TreeNode* root) {
        getSubTreeSum(root);
        return res;
    }
    int getSubTreeSum(TreeNode* root) {
        if (root == NULL)
            return 0;
        int leftSum = getSubTreeSum(root->left),
            rightSum = getSubTreeSum(root->right);
        res += abs(leftSum - rightSum);
        return leftSum + rightSum + root->val;
    }
private:
    int res = 0;
};

int main() {
    TreeNode *node1 = new TreeNode(1);
    TreeNode *node2 = new TreeNode(2);
    TreeNode *node3 = new TreeNode(3);
    TreeNode *node4 = new TreeNode(4);
    TreeNode *node5 = new TreeNode(5);

    node1->left = node2;
    node1->right = node3;
    node2->left = node4;
    node3->right = node5;

    Solution s;

    cout << s.findTilt(node1) << endl;

    system("pause");
    return 0;
}
【Boost5】Boost变换器的输入电容 节点的电流有3个,一个是来自电源输入的,由于我们把Vin看成理想电源以及线路的电感的存在,它可以近似看作是恒定的电流源Iin,一个是流向输入电容Cin的电流Icin,另外一个节点是电感L电流IL。电容电流波形在整个开关周期内的平均值为0,充电和放电在一个周期中各占1/2,在图中充电部分面积和放电部分面积相等,那么在充电或放电的电荷量Q=I*t,从图中可以看出,在一个周期内电容电流变化量是ΔIL,那在电容在充电1/2T周期中电流的变化量是1/2ΔIL。输入电容上的ESR也会随着电流变化,影响输入电压的变化。 阅读详情

相关推荐

论文理解【LLM-OR】——【OptMATH】A Scalable Bidirectional Data Synthesis Framework for Optimization Modeling

本文提出 LLM-OR 领域的双向数据合成框架 OptMATH,从已有 MF/PD 出发反向生成 NL,再使用 AutoFormulator 进行正向建模,通过最优值匹配 rejection sampling 确保语义一致性。实现了难度可控的语义一致数据合成

佚失的诗篇 511

LeetCode //C - 563. Binary Tree Tilt

【代码】LeetCode //C - 563. Binary Tree Tilt。

Made in Code 806

UVM自学笔记3——项目之三——从零开始搭建ahb_sramc验证平台

创建如下所示的目录结构: 2.在tb的文件下:顶层的testbench如下:在tb中,第24行的代码将uvm宏相关的内容include进来,第26行的代码把uvm_pkg包导入进来,然后将agent相关的包(ahb_pkg/sram_pkg),testcase相关的包(uvm_ahb_sram_test_pkg)以及env相关的包(uvm_ahb_sram_env_pkg)都导入进来,在第38行和39行将接口以virtual interface的形式配置给ahb_agent和sramc_agent下

ZhangShuHuai11的博客 9492

LeetCode 563 Binary Tree Tilt

LeetCode 563 Binary Tree Tilt Problem Description: 给出一棵二叉树,计算每个节点左右子树所有结点值的和,对两个值作差取绝对值,再将所有结点计算得到的绝对值相加。 具体的题目信息: https://leetcode.com/problems/binary-tree-tilt/description/ Solution: /** * ...

萤火虫啊飞呀飞 251

【迁移学习综述】

迁移学习的目标是将某个领域或任务上学习到的知识应用到不同的但相关的领域或问题中。迁移学习的研究来源于一个观测:人类可以将以前的学到的知识应用于解决新的问题,更快的解决问题或取得更好的效果。迁移学习被赋予这样一个任务:从以前的任务当中去学习知识(knowledge)或经验,并应用于新的任务当中。换句话说,迁移学习目的是从一个或多个源任务(source tasks)中抽取知识、经验,然后应用于一个目标领域(target domain)当中去。

afadgas的博客 4328

【亲测有效】OpenClaw Gateway 出现 “(no output)“ 卡死问题完整解决指南:从手动重启到 systemd Watchdog 自动恢复

最近在使用 OpenClaw(自托管 AI 助手框架,前身 Clawdbot/Moltbot)时,遇到了一个非常常见的顽疾:TUI(终端界面)或 Telegram 等通道经常显示 "(no output)",消息发出去后机器人完全没反应,看起来像是卡死了。

hampeter的博客 8630

leetcode 563. Binary Tree Tilt

Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtre

淡然坊 573

基于Simulink的超级电容器充放电仿真

为了更好地理解和设计超级电容器的应用系统,通常需要进行详细的仿真分析。Simulink 提供了一个强大的仿真环境,可以用来设计、分析和验证超级电容器充放电系统的控制策略。本教程将详细介绍如何使用 Simulink 进行超级电容器充放电的仿真实例。如果你希望对超级电容器的充放电过程进行精确控制,可以添加一个简单的逻辑控制器来控制开关的状态。通过本教程,我们详细介绍了如何使用 Simulink 进行超级电容器充放电的仿真实例。2. 添加超级电容器模型。7. 添加控制器(可选)1. 调整超级电容器参数。

xiaoheshang_123的博客 523

LeetCode 563. Binary Tree Tilt

LeetCode 563. Binary Tree Tilt (Easy) 主要知识点:树、postorder;优先级:4(想想递归最优解)

温迪今天刷题了吗 1954

多个linux系统安装到一块硬盘(Kylin+UOS+ubuntu)

之前写过一篇类似的文档,笔者把kylin和UOS安装在一块硬盘上上一篇的文档地址多个linux系统安装到一块硬盘(Kylin+UOS)_银河麒麟系统和uos安装成双系统-CSDN博客本次记录的是三个系统到一块硬件上银河麒麟:Kylin-Desktop-V10-SP1-Release-2107-arm64统信:uniontechos-desktop-20-professional-1050-arm64乌班图:ubuntu-22.04.5-live-server-arm64。

aggs1990的专栏 577

【100%通过率】华为OD机试真题 Python 实现【网上商城优惠活动 / 模拟商场优惠打折II】

某网上商场举办优惠活动,发布了满减、打折、无门槛3种 优惠券,分别为: 1:每满100元优惠10元,无使用数限制,如100~199元可以使用1张减10元,200~299可使用2张减20元,以此类推; 2:92折券,1次限使用1张,如100元,则优惠后为92元, 3:无门槛5元优惠券,无使用数限制,直接减5元。

MISAYAONE的博客 9383

【Leetcode】563. Binary Tree Tilt

求每个节点左右子树节点和差 方法1 递归+全局变量 求出每个节点下的节点和即可 class Solution1: def findTilt(self, root): &quot;&quot;&quot; :type root: TreeNode :rtype: int &quot;&quot;&quot; self.res = 0 self.getSu...

很吵请安静 401

Python爬虫实战:利用Selenium与异步技术高效获取雪球财经数据

雪球财经作为国内领先的投资社交平台,汇聚了大量股票数据、财经资讯和投资者观点。对于金融数据分析师、量化投资者和研究人员来说,获取雪球平台的实时数据具有重要价值。然而,雪球网站采用了多种反爬虫机制,包括动态内容加载、请求频率限制和用户行为检测等,使得传统爬虫技术难以有效获取数据。本文将介绍如何使用Python的最新爬虫技术,包括Selenium WebDriver、异步请求处理和智能等待策略,来高效、稳定地爬取雪球财经数据。我们将重点解决以下几个技术难点:动态加载内容的处理登录状态维持与模拟。

2201_76125261的博客 1111

Leetcode——563. Binary Tree Tilt

题目描述 Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right su...

想当厨子的程序媛 430

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LEETCODE(563. Binary Tree Tilt)

563. Binary Tree Tilt题目描述给定一个二叉树,计算它的tilt。每个节点的tilt是它左子树节点之和与右子树节点之和的差的绝对值。null视为0,示例如下。Input: 1 / \ 2 3 Output: 1 Explanation: Tilt of node 2 : 0 Tilt of node 3 : 0 Tilt...

pumpkin-- 284

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

leetcode-563. Binary Tree Tilt

563. Binary Tree Tilt Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and

link98 280

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【leetcode】563. Binary Tree Tilt总结

「思路」 设定两个函数,tilt,sum。tilt负责收集左右的和,并相减,并将所有相减的结果想加。sum负责纵向的相加,算出左右字树数字的和。分类按照有无左右子数分类。 「运行结果」 class Solution { public: int findTilt(TreeNode* root) { if(!root)return 0; return til

qq_31697271的博客 366

Leetcode 563. Binary Tree Tilt

问题描述: Given the root of a binary tree, return the sum of every tree node’s tilt. 计算二叉树所有节点tilt值之和。tilt值是一个节点左子树之和与右子树之和的差的绝对值 思路: 用中序遍历访问二叉树的每个节点。在每个节点上计算tilt值 (计算tilt值需要再次用到递归subLeft-subRight) 代码如下: class Solution { int total=0; public int findTi

weixin_51401145的博客 317

[leetcode]563. Binary Tree Tilt

[leetcode]563. Binary Tree Tilt Analysis 中午吃啥?—— [明天要回家啦,嘻嘻~] Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between t...

weixin_32135877的博客 156

[LeetCode][563] Binary Tree Tilt题解

[LeetCode][563] Binary Tree Tilt题解 给定一个树,定义一个树的左右子树的差值的绝对值为树的坡度。 要求返回树的所有节点的坡度的和 dfs搜索左右子树的和 然后相减取绝对值,将这个结果累加到全局变量res上 dfs作用即是记录当前树的总和,以便它的父亲调用(回溯时候调用) /* * [563] Binary Tree Tilt * * https://l...

OCEANtroye的博客 329

LeetCode 563. Binary Tree Tilt (C++)

Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree

haotiangg的专栏 481

[LeetCode] 563. Binary Tree Tilt

原题链接:

Ber03的博客 233

LeetCode-563. Binary Tree Tilt

Description Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all ...

zy2317878的博客 210
上一篇: [LeetCode]438. Find All Anagrams in a String
下一篇: [LeetCode]561. Array Partition I
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值