Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 12, 2023
1 parent a2e8742 commit 10e2ef2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions leetcode/563.BinaryTreeTilt/binaryTreeTilt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func findTilt(root *TreeNode) int {
result := 0
dfs(root, &result)
return result
}

func dfs(root *TreeNode, result *int) int {
if root == nil {
return 0
}
left, right := dfs(root.Left, result), dfs(root.Right, result)
*result += abs(left - right)
return root.Val + right + left
}

func abs(a int) int {
if a > 0 {
return a
}
return -a
}

func main() {}

0 comments on commit 10e2ef2

Please sign in to comment.