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 ... 阅读详情

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 node values. Null node has tilt 0.

The tilt of the whole tree is defined as the sum of all nodes’ tilt.

Example:

Input: 
         1
       /   \
      2     3
Output: 1
Explanation: 
Tilt of node 2 : 0
Tilt of node 3 : 0
Tilt of node 1 : |2-3| = 1
Tilt of binary tree : 0 + 0 + 1 = 1

Note:

The sum of node values in any subtree won’t exceed the range of 32-bit integer.
All the tilt values won’t exceed the range of 32-bit integer.

/**
这题其实要你求每个节点的左右子树的差的绝对值,再对这些绝对值求和.简单递归搜索,16ms AC
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sum = 0;

    int dfs(TreeNode *root)             //表示以root为根节点的所有子节点(包括本身)之和
    {
        if(root == NULL)
            return 0;
        int left = dfs(root->left), right = dfs(root->right);
        sum += abs(left-right);         //左右子树的差的绝对值
        return left+right+root->val;
    }
    int findTilt(TreeNode* root) {
        if(root == NULL)
            return 0;
        dfs(root);

        return sum;
    }
};
如何使你的直流电机闭环?(PID讲解) 看了看很多大佬写的PID讲解很全面也很复杂,实在是不适合很多萌新入坑,所以想按自己的理解写一篇通俗易懂的PID算法讲解 阅读详情

相关推荐

从CPU视角看PCIe:深入理解x86/ARM平台上BAR、MMIO和PIO的地址翻译与访问机制

本文深入探讨了CPU如何通过BAR、MMIO和PIO机制访问PCIe设备的配置空间,详细解析了x86和ARM平台在地址翻译与访问路径上的差异。重点分析了PCIe配置空间中BAR寄存器的作用与探测机制,以及操作系统如何管理PCIe资源,为开发者提供了性能优化与调试的实用技巧。

weixin_30175731的博客 325

【leetcode】563. Binary Tree Tilt总结

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

qq_31697271的博客 365

Mac用户看过来!M1/M2芯片安装CiteSpace完整指南(从Homebrew到成功运行)

本文为Mac M1/M2芯片用户提供CiteSpace安装的完整指南,从Homebrew安装适配的Java环境到最终成功运行CiteSpace。详细步骤包括环境检查、Java配置、CiteSpace下载与优化,解决常见问题如内存分配、图形渲染和中文显示乱码,确保科研人员能顺利使用这一文献可视化工具。

weixin_42530458的博客 308

563. Binary Tree Tilt

Problem 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 s...

Bryan要加油 511

Chapter 19 Digital Phase-Locked Loops

数字锁相环 Digital Phase-Locked Loops, 就是根据原来时钟信号, 产生新的时钟信号, which is locked or synchronized. (align 参考时钟边沿 or 在中间)

2304_76736886的博客 973

RocketMq面板访问异常 com.alibaba.fastjson.JSONException: not close json text, token : string

rocketmq JSONException: not close json text, token : string 问题解决

qq_21067307的博客 5575

LeetCode 563. Binary Tree Tilt

题意:计算二叉树的Tilt,二叉树结点的Tilt是其左右子树结点value值的和的差的绝对值,整个二叉树的Tile是所有结点的Tilt之和。 题解:采用后序遍历递归求解左右子树的Tilt, 后序遍历函数postorder(root)返回左右子树value和,并加上该Node的val,作为root的左右子树sum。 /** * Definition for a binary tree node

zengquan299的博客 700

拯救者Y9000P 2022双系统实战:Ubuntu22.04分区优化与驱动避坑指南

本文提供联想拯救者Y9000P 2022款安装Ubuntu 22.04双系统的详细实战指南。重点讲解针对该机型的磁盘分区优化方案、NVIDIA显卡驱动安装避坑技巧,以及安装后的网络、时间同步等必备配置,帮助用户高效完成双系统部署并提升使用体验。

weixin_29208327的博客 427

Add to List 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 subtree nod

lisa___33的博客 569

华为od机试|华为od机试真题2025|华为od题库|华为od机考|2025C卷B卷

华为od机试|华为od机试真题2025|华为od题库|华为od机考|华为od机试|华为od机试真题2025|华为od题库|华为od机考|华为od机试|华为od机试真题2025|华为od题库|华为od机考|华为od机试|华为od机试真题2025|华为od题库|华为od机考|B卷C卷

u014481728的博客 10万+

ABAP RANGES及比较符号

RANGES *选项介绍: *1.SIGN 值为 I 和 E 。 I是包含, E是排除, 一般使用I。 2.OPTION 如果HIGH 为空 ,为单值选择 。有 EQ、NE、GT、LE、LT等逻辑操作 对于的 CP包含 NP是排除 如果HIGH 不为空 为区间选择 有BT,NB可选 *3.LOW 低值 *4.HIGH 高值 lt:less than 小于 le:less than or equal to 小于等于 eq:equal to 等于 ne:

Pegasus666的博客 5393

LeetCode //C - 563. Binary Tree Tilt

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

Made in Code 806

STM32G0/G4电机控制中三角函数与浮点运算优化实战

三角函数(sin/cos)和浮点运算是电机矢量控制(FOC)的核心基础,直接影响Clark/Park变换、SVPWM及观测器的实时性与精度。在资源受限的MCU上,标准math.h库因无硬件FPU支持而引发严重时序抖动,导致电流环延迟、转矩脉动增大甚至系统复位。通过硬件加速(如STM32G4的CORDIC协处理器)或定制化定点算法(如STM32G0的查表+线性插值),可将sin/cos计算从数十微秒压缩至1–3 μs量级,提升30倍以上效率。该方案广泛适用于BLDC/PMSM驱动、伺服控制及高动态响应嵌入式运

weixin_35751412的博客 112

LeetCode 563 Binary Tree Tilt

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

萤火虫啊飞呀飞 251

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

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

淡然坊 570

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【Leetcode】563. Binary Tree Tilt

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

很吵请安静 401

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

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

[LeetCode]563. Binary Tree Tilt

[LeetCode]563. Binary Tree Tilt题目描述思路深搜 因为是要计算左右子树节点和之间的绝对值,所以递归的返回值是子树节点的和(即需要拆两个函数)。 在递归的同时计算结果,注意因为是递归调用函数,因此需要临时变量来保存调用结果,不可以多次调用代码#include <iostream>using namespace std;struct TreeNode { int

Lcharon的博客 643

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

OCEANtroye的博客 329

[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 (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 | 20. Valid Parentheses
下一篇: LeetCode | 21. Merge Two Sorted Lists
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值