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
}
相关推荐
sprintzer44 分钟前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
Tony Bai1 小时前
【Go 网络编程全解】06 UDP 数据报编程:速度、不可靠与应用层弥补
开发语言·网络·后端·golang·udp
Dobby_051 小时前
【Go】C++ 转 Go 第(一)天:环境搭建 Windows + VSCode 远程连接 Linux
linux·运维·c++·vscode·golang
JCGKS1 小时前
Go|sync.Pool|临时对象池,实现临时对象的复用,降低GC压力
golang·对象池·对象复用·降低gc压力
一匹电信狗2 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
学学学无无止境2 小时前
力扣-上升的温度
leetcode
Emilia486.4 小时前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
Dream it possible!6 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏6 小时前
49.字母异位词分组
java·开发语言·leetcode
Espresso Macchiato6 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471