一、题目
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
二、题目大意
二叉搜索树中是否存在两个元素的值为k。
三、解题思路
递归遍历二叉树。
四、代码实现
const findTarget = (root, k) => {
const ans = []
help(root)
const max = ans.length
for (let i = 0; i < max; i++) {
for (let j = i + 1; j < max; j++) {
const pre = ans[i]
const next = ans[j]
if (pre + next === k) {
return true
}
}
}
return false
function help (root) {
if (!root) {
return
}
help(root.left)
ans.push(root.val)
help(root.right)
}
}
如果本文对您有帮助,欢迎关注微信公众号,为您推送更多大前端相关的内容, 欢迎留言讨论,ε=ε=ε=┏(゜ロ゜;)┛。

您还可以在这些地方找到我: