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
}
相关推荐
YoungHong19926 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER7 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
LYFlied8 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
LYFlied11 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
zfj32112 小时前
vscode是js开发的,为什么能支持golang java等各种语言开发
javascript·vscode·golang
努力学算法的蒟蒻12 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
serendipity_hky13 小时前
【go语言 | 第5篇】channel——多个goroutine之间通信
开发语言·后端·golang
LYFlied13 小时前
【每日算法】LeetCode 234. 回文链表详解
算法·leetcode·链表
源代码•宸13 小时前
分布式缓存-GO(简历写法、常见面试题)
服务器·开发语言·经验分享·分布式·后端·缓存·golang