-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
530. Minimum Absolute Difference in BST
- Loading branch information
1 parent
133d943
commit a40f691
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
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
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,46 @@ | ||
//https://leetcode.com/problems/binary-tree-inorder-traversal/ | ||
|
||
namespace LeetCode.Problems; | ||
|
||
public sealed class GetMinimumDifference : ProblemBase | ||
{ | ||
[Theory] | ||
[ClassData(typeof(GetMinimumDifference))] | ||
public override void Test(object[] data) => base.Test(data); | ||
|
||
protected override void AddTestCases() | ||
=> Add(it => it.ParamTree("[4,2,6,1,3]").Result(1)) | ||
.Add(it => it.ParamTree("[1,0,48,null,null,12,49]").Result(1)) | ||
.Add(it => it.ParamTree("[10,0,48,null,null,16,56]").Result(6)); | ||
|
||
private int Solution(TreeNode? root) | ||
{ | ||
var stack = new Stack<TreeNode>(); | ||
TreeNode? prevNode = null; | ||
var diff = int.MaxValue; | ||
|
||
var node = root; | ||
while (node != null || stack.Any()) | ||
{ | ||
if (node != null) | ||
{ | ||
stack.Push(node); | ||
node = node.left; | ||
} | ||
else | ||
{ | ||
var pop = stack.Pop(); | ||
if (prevNode != null) | ||
{ | ||
diff = Math.Min(diff, pop.val - prevNode.val); | ||
} | ||
|
||
prevNode = pop; | ||
|
||
node = pop.right; | ||
} | ||
} | ||
|
||
return diff; | ||
} | ||
} |
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,53 @@ | ||
// https://leetcode.com/problems/minimum-absolute-difference-in-bst | ||
|
||
package problems | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/dobermanch/leetcode/core" | ||
) | ||
|
||
type GetMinimumDifference struct{} | ||
|
||
func TestGetMinimumDifference(t *testing.T) { | ||
gen := core.TestSuite[GetMinimumDifference]{} | ||
gen.Add(func(tc *core.TestCase) { | ||
root := &TreeNode{ | ||
Val: 10, | ||
Left: &TreeNode{Val: 0, Left: &TreeNode{Val: 4}}, | ||
Right: &TreeNode{Val: 48, Left: &TreeNode{Val: 16}, Right: &TreeNode{Val: 56}}, | ||
} | ||
tc.Param(root).Result(6) | ||
}).Run(t) | ||
} | ||
|
||
func (GetMinimumDifference) Solution(root *TreeNode) int { | ||
stack := []*TreeNode{} | ||
diff := math.MaxInt | ||
|
||
node := root | ||
var prevNode *TreeNode | ||
for node != nil || len(stack) > 0 { | ||
if node != nil { | ||
stack = append(stack, node) | ||
node = node.Left | ||
} else { | ||
pop := stack[len(stack)-1] | ||
stack = stack[:len(stack)-1] | ||
|
||
if prevNode != nil { | ||
newDiff := pop.Val - prevNode.Val | ||
if newDiff < diff { | ||
diff = newDiff | ||
} | ||
} | ||
|
||
prevNode = pop | ||
node = pop.Right | ||
} | ||
} | ||
|
||
return diff | ||
} |
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,34 @@ | ||
# https://leetcode.com/problems/minimum-absolute-difference-in-bst | ||
|
||
from typing import Optional | ||
from core.problem_base import * | ||
from models.tree_node import TreeNode | ||
|
||
class GetMinimumDifference(ProblemBase): | ||
def Solution(self, root: Optional[TreeNode]) -> int: | ||
stack = [] | ||
diff = 10**5 | ||
|
||
node = root | ||
prevNode = None | ||
while node or len(stack) > 0: | ||
if node: | ||
stack.append(node) | ||
node = node.left | ||
else: | ||
pop = stack.pop() | ||
|
||
if prevNode: | ||
diff = min(diff, pop.val - prevNode.val) | ||
|
||
prevNode = pop | ||
node = pop.right | ||
|
||
return diff | ||
|
||
if __name__ == '__main__': | ||
TestGen(GetMinimumDifference) \ | ||
.Add(lambda tc: tc.ParamTreeNode([4,2,6,1,3]).Result(1)) \ | ||
.Add(lambda tc: tc.ParamTreeNode([1,0,48,None,None,12,49]).Result(1)) \ | ||
.Add(lambda tc: tc.ParamTreeNode([10,0,48,None,None,16,56]).Result(6)) \ | ||
.Run() |