1 Star 0 Fork 0

孙启萌 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
563.二叉树的坡度.js 980 Bytes
一键复制 编辑 原始数据 按行查看 历史
孙启萌 提交于 2023-05-22 15:06 . 刷题
/*
* @lc app=leetcode.cn id=563 lang=javascript
*
* [563] 二叉树的坡度
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var findTilt = function(root) {
let res = 0
function fun(root) {
if (!root) {
return 0
}
if (!root.left && !root.right) {
return root.val
}
let left = fun(root.left)
let right = fun(root.right)
res += Math.abs(left - right)
return left + right + root.val
}
fun(root)
return res
};
// @lc code=end
/*
耻辱,想了那么久就是感觉临门一脚想不出来还要看题解,结果发现是因为 res 初始定义的不是 0 而是空字符串导致的
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqm147896325/leetcode.git
git@gitee.com:sqm147896325/leetcode.git
sqm147896325
leetcode
leetcode
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891