parent
9048bc281e
commit
4b1fbe9bc9
@ -0,0 +1,27 @@
|
||||
653. Two Sum IV - Input is a BST
|
||||
Easy
|
||||
|
||||
Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: root = [5,3,6,2,4,null,7], k = 9
|
||||
Output: true
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: root = [5,3,6,2,4,null,7], k = 28
|
||||
Output: false
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
The number of nodes in the tree is in the range [1, 104].
|
||||
-104 <= Node.val <= 104
|
||||
root is guaranteed to be a valid binary search tree.
|
||||
-105 <= k <= 105
|
||||
|
||||
|
@ -0,0 +1,42 @@
|
||||
package twosumiv
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func lookup(node *TreeNode, target int) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if node.Val == target {
|
||||
return true
|
||||
}
|
||||
|
||||
if target < node.Val {
|
||||
return lookup(node.Left, target)
|
||||
}
|
||||
|
||||
return lookup(node.Right, target)
|
||||
}
|
||||
|
||||
func recur(node *TreeNode, root *TreeNode, k int) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
remaining := k - node.Val
|
||||
|
||||
return (remaining != node.Val && lookup(root, remaining)) ||
|
||||
recur(node.Left, root, k) ||
|
||||
recur(node.Right, root, k)
|
||||
}
|
||||
|
||||
func findTarget(root *TreeNode, k int) bool {
|
||||
return recur(root, root, k)
|
||||
}
|
||||
|
||||
func FindTarget(root *TreeNode, k int) bool {
|
||||
return findTarget(root, k)
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package twosumiv_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ts "git.jxs.me/leetgo/twosumiv"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
var root *ts.TreeNode
|
||||
found := ts.FindTarget(root, 1)
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestShallow(t *testing.T) {
|
||||
root := ts.TreeNode{
|
||||
Val: 5,
|
||||
Left: &ts.TreeNode{Val: 4},
|
||||
Right: &ts.TreeNode{Val: 6},
|
||||
}
|
||||
found := ts.FindTarget(&root, 10)
|
||||
require.True(t, found)
|
||||
}
|
||||
|
||||
func TestShallowMissing(t *testing.T) {
|
||||
root := ts.TreeNode{
|
||||
Val: 5,
|
||||
Left: &ts.TreeNode{Val: 4},
|
||||
Right: &ts.TreeNode{Val: 6},
|
||||
}
|
||||
found := ts.FindTarget(&root, 12)
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestExample1(t *testing.T) {
|
||||
root := ts.TreeNode{
|
||||
Val: 5,
|
||||
Left: &ts.TreeNode{Val: 3,
|
||||
Left: &ts.TreeNode{Val: 2},
|
||||
Right: &ts.TreeNode{Val: 4}},
|
||||
Right: &ts.TreeNode{Val: 6,
|
||||
Right: &ts.TreeNode{Val: 7}},
|
||||
}
|
||||
found := ts.FindTarget(&root, 9)
|
||||
require.True(t, found)
|
||||
}
|
||||
|
||||
func TestExample2(t *testing.T) {
|
||||
root := ts.TreeNode{
|
||||
Val: 5,
|
||||
Left: &ts.TreeNode{Val: 3,
|
||||
Left: &ts.TreeNode{Val: 2},
|
||||
Right: &ts.TreeNode{Val: 4}},
|
||||
Right: &ts.TreeNode{Val: 6,
|
||||
Right: &ts.TreeNode{Val: 7}},
|
||||
}
|
||||
found := ts.FindTarget(&root, 28)
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestHalf(t *testing.T) {
|
||||
root := ts.TreeNode{
|
||||
Val: 5,
|
||||
Left: &ts.TreeNode{Val: 3,
|
||||
Left: &ts.TreeNode{Val: 2},
|
||||
Right: &ts.TreeNode{Val: 4}},
|
||||
Right: &ts.TreeNode{Val: 6,
|
||||
Right: &ts.TreeNode{Val: 7}},
|
||||
}
|
||||
found := ts.FindTarget(&root, 14)
|
||||
require.False(t, found)
|
||||
}
|
Loading…
Reference in new issue