Skip to content

Commit 20127ef

Browse files
authored
Create 1214-two-sum-bsts.js
1 parent 94a31a3 commit 20127ef

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1214-two-sum-bsts.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root1
11+
* @param {TreeNode} root2
12+
* @param {number} target
13+
* @return {boolean}
14+
*/
15+
const twoSumBSTs = function(root1, root2, target) {
16+
if(root1 == null && root2 == null) return false
17+
if(root2 == null) return root1.val === target
18+
if(root1 == null) return root2.val === target
19+
if(root1.val + root2.val === target) return true
20+
if(root1.val + root2.val < target) {
21+
return twoSumBSTs(root1.right, root2, target) || twoSumBSTs(root1, root2.right, target)
22+
} else {
23+
return twoSumBSTs(root1.left, root2, target) || twoSumBSTs(root1, root2.left, target)
24+
}
25+
};

0 commit comments

Comments
 (0)