Golang | Leetcode Golang题解之第563题二叉树的坡度

题目:

题解:

Go 复制代码
func findTilt(root *TreeNode) (ans int) {
    var dfs func(*TreeNode) int
    dfs = func(node *TreeNode) int {
        if node == nil {
            return 0
        }
        sumLeft := dfs(node.Left)
        sumRight := dfs(node.Right)
        ans += abs(sumLeft - sumRight)
        return sumLeft + sumRight + node.Val
    }
    dfs(root)
    return
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
相关推荐
想搞艺术的程序员25 分钟前
深入 NSQ 延迟消息实现原理:设计巧思与性能优化
性能优化·golang·nsq
leoufung5 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
Aspect of twilight7 小时前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
2301_807997387 小时前
代码随想录-day47
数据结构·c++·算法·leetcode
Elias不吃糖7 小时前
LeetCode每日一练(3)
c++·算法·leetcode
小年糕是糕手12 小时前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
(づど)13 小时前
解决VSCode中安装Go环境Gopls失败的问题
vscode·golang
sheeta199817 小时前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode
橘颂TA1 天前
【剑斩OFFER】算法的暴力美学——两整数之和
算法·leetcode·职场和发展
Dream it possible!1 天前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树的最小绝对差(85_530_C++_简单)
c++·leetcode·面试