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
}
相关推荐
退休倒计时11 小时前
【每日一题】LeetCode 53. 最大子数组和 TypeScript
数据结构·算法·leetcode·typescript
张忠琳13 小时前
【Go 1.26.4】Golang Channel 深度解析
开发语言·后端·golang
洛水水14 小时前
【力扣100题】86.柱状图中最大的矩形
算法·leetcode·职场和发展
洛水水15 小时前
【力扣100题】81.寻找两个正序数组的中位数
数据结构·算法·leetcode
张忠琳15 小时前
【Go 1.26.4】Golang Map 深度解析
开发语言·后端·golang
洛水水16 小时前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Kurisu_红莉栖16 小时前
力扣56合并区间
算法·leetcode
开源Z16 小时前
LeetCode 135 · 分发糖果:两次扫描,先左后右取最大
算法·leetcode
退休倒计时16 小时前
【每日一题】LeetCode 19. 删除链表的倒数第 N 个结点 TypeScript
leetcode·链表·typescript
怪兽学LLM19 小时前
LeetCode 21 合并两个有序链表:彻底理解虚拟头节点(Dummy)套路
python·leetcode·链表