-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maximum Difference Between Node and Ancestor
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...imum Difference Between Node and Ancestor/Maximum Difference Between Node and Ancestor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package problem1026 | ||
|
||
type TreeNode struct { | ||
Val int | ||
Left *TreeNode | ||
Right *TreeNode | ||
} | ||
|
||
func abs(num int) int { | ||
if num < 0 { | ||
return -num | ||
} | ||
return num | ||
} | ||
|
||
func getMax(nums ...int) int { | ||
max := nums[0] | ||
for _, num := range nums { | ||
if max < num { | ||
max = num | ||
} | ||
} | ||
return max | ||
|
||
} | ||
func getMin(nums ...int) int { | ||
min := nums[0] | ||
for _, num := range nums { | ||
if min > num { | ||
min = num | ||
} | ||
} | ||
return min | ||
|
||
} | ||
func dFS(root *TreeNode) (min, max, diff int) { | ||
if root == nil { | ||
return 100001, -1, 0 | ||
} | ||
lMin, lMax, lDiff := dFS(root.Left) | ||
rMin, rMax, rDiff := dFS(root.Right) | ||
min = getMin(lMin, rMin) | ||
max = getMax(lMax, rMax) | ||
diff = getMax(lDiff, rDiff) | ||
if min < 100001 { | ||
diff = getMax(diff, abs(root.Val-min)) | ||
} | ||
if max > -1 { | ||
diff = getMax(diff, abs(root.Val-max)) | ||
} | ||
|
||
min = getMin(min, root.Val) | ||
max = getMax(lMax, rMax, root.Val) | ||
return | ||
|
||
} | ||
func maxAncestorDiff(root *TreeNode) int { | ||
_, _, res := dFS(root) | ||
return res | ||
|
||
} |