2023-12-09 18:42:21 +08:00
< p > Given the < code > root< / code > of a binary search tree and an integer < code > k< / code > , return < code > true< / code > < em > if there exist two elements in the BST such that their sum is equal to< / em > < code > k< / code > , < em > or< / em > < code > false< / code > < em > otherwise< / em > .< / p >
2022-03-27 20:46:41 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< img alt = "" src = "https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg" style = "width: 400px; height: 229px;" / >
< pre >
< strong > Input:< / strong > root = [5,3,6,2,4,null,7], k = 9
< strong > Output:< / strong > true
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< img alt = "" src = "https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg" style = "width: 400px; height: 229px;" / >
< pre >
< strong > Input:< / strong > root = [5,3,6,2,4,null,7], k = 28
< strong > Output:< / strong > false
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > The number of nodes in the tree is in the range < code > [1, 10< sup > 4< / sup > ]< / code > .< / li >
2023-12-09 18:42:21 +08:00
< li > < code > -10< sup > 4< / sup > < = Node.val < = 10< sup > 4< / sup > < / code > < / li >
2022-03-27 20:46:41 +08:00
< li > < code > root< / code > is guaranteed to be a < strong > valid< / strong > binary search tree.< / li >
2023-12-09 18:42:21 +08:00
< li > < code > -10< sup > 5< / sup > < = k < = 10< sup > 5< / sup > < / code > < / li >
2022-03-27 20:46:41 +08:00
< / ul >