From 2bb078c03a91b612d0e57162de93a0fdcbade117 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Tue, 18 Feb 2020 19:44:58 -0700 Subject: [PATCH 1/2] day 453: cleaned up day 125 --- day453/problem.go | 46 ++++++++++++++++++++++++++++++++++++++++++ day453/problem_test.go | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 day453/problem.go create mode 100644 day453/problem_test.go diff --git a/day453/problem.go b/day453/problem.go new file mode 100644 index 0000000..6a4bd1a --- /dev/null +++ b/day453/problem.go @@ -0,0 +1,46 @@ +package day453 + +// BST is an integer binary search tree. +type BST struct { + Val int + Left, Right *BST +} + +// FindTwoSumKInBST returns a slice of integer nodes that sum to K. +// If no two nodes exist that sum to K, then return nil. +// Runs in O(N log N) where N is the size of the BST. +func FindTwoSumKInBST(head *BST, k int) []int { + return inorder(head, head, k, []int{}) +} + +func inorder(head, current *BST, k int, result []int) []int { + if current == nil { + return result + } + + if leftResult := inorder(head, current.Left, k, result); len(leftResult) == 2 { + return leftResult + } + + if node := searchBST(head, k-current.Val); node != nil && node != current { + return []int{current.Val, node.Val} + } + + if rightResult := inorder(head, current.Right, k, result); len(rightResult) == 2 { + return rightResult + } + + return result +} + +func searchBST(head *BST, target int) *BST { + if head == nil { + return nil + } + + if target == head.Val { + return head + } + + return searchBST(head.Right, target) +} diff --git a/day453/problem_test.go b/day453/problem_test.go new file mode 100644 index 0000000..a2e3d62 --- /dev/null +++ b/day453/problem_test.go @@ -0,0 +1,44 @@ +package day453 + +import ( + "reflect" + "testing" +) + +// nolint +var testcases = []struct { + head *BST + k int + expected []int +}{ + {&BST{10, &BST{5, nil, nil}, &BST{ + 15, &BST{11, nil, nil}, &BST{15, nil, nil}, + }}, 20, []int{5, 15}}, + {&BST{10, &BST{5, nil, nil}, &BST{ + 15, &BST{11, nil, nil}, &BST{15, nil, nil}, + }}, 100, []int{}}, + {&BST{10, &BST{5, nil, nil}, &BST{ + 15, &BST{11, nil, nil}, &BST{15, nil, nil}, + }}, 15, []int{5, 10}}, + {&BST{10, &BST{5, nil, nil}, &BST{ + 15, &BST{11, nil, nil}, &BST{15, nil, nil}, + }}, 26, []int{11, 15}}, +} + +func TestFindTwoSumKInBST(t *testing.T) { + t.Parallel() + + for _, tc := range testcases { + if result := FindTwoSumKInBST(tc.head, tc.k); !reflect.DeepEqual(result, tc.expected) { + t.Errorf("Expected %v got %v", tc.expected, result) + } + } +} + +func BenchmarkFindTwoSumInBST(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tc := range testcases { + FindTwoSumKInBST(tc.head, tc.k) + } + } +} From 1356454bcd92fc149aafa31d19e9d317656347b8 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Tue, 18 Feb 2020 19:45:19 -0700 Subject: [PATCH 2/2] add day 453 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index af36924..4b03bdf 100644 --- a/README.md +++ b/README.md @@ -450,4 +450,5 @@ problems from * [Day 450](https://github.com/vaskoz/dailycodingproblem-go/issues/909) * [Day 451](https://github.com/vaskoz/dailycodingproblem-go/issues/911) * [Day 452](https://github.com/vaskoz/dailycodingproblem-go/issues/913) +* [Day 453](https://github.com/vaskoz/dailycodingproblem-go/issues/915)