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
}
相关推荐
Tim_101 小时前
【LeetCode】338、比特位计数
c++·算法·leetcode
mmmmath_32 小时前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Navigator_Z3 小时前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.3 小时前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
开开心心就好3 小时前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法
All for pursuit.3 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
圣保罗的大教堂4 小时前
leetcode 1665. 完成所有任务的最少初始能量 中等
leetcode
Tisfy19 小时前
LeetCode 3498.字符串的反转度:一次遍历
leetcode·字符串·题解·遍历
a1879272183119 小时前
【算法】树(二):队列与祖先——BFS 骨架三配件、短路透传与合流
算法·leetcode·二叉树··bfs·算法讲解·树遍历
Navigator_Z1 天前
LeetCode //C - 1254. Number of Closed Islands
c语言·算法·leetcode