comments | difficulty | edit_url | rating | source | tags | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1389 |
第 10 场双周赛 Q2 |
|
给出两棵二叉搜索树的根节点 root1
和 root2
,请你从两棵树中各找出一个节点,使得这两个节点的值之和等于目标值 Target
。
如果可以找到返回 True
,否则返回 False
。
示例 1:
输入:root1 = [2,1,4], root2 = [1,0,3], target = 5 输出:true 解释:2 加 3 和为 5 。
示例 2:
输入:root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18 输出:false
提示:
- 每棵树上节点数在
[1, 5000]
范围内。 -109 <= Node.val, target <= 109
我们分别对两棵树进行中序遍历,得到两个有序数组
初始化两个指针
每次比较 true
;否则,如果
时间复杂度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def twoSumBSTs(
self, root1: Optional[TreeNode], root2: Optional[TreeNode], target: int
) -> bool:
def dfs(root: Optional[TreeNode], i: int):
if root is None:
return
dfs(root.left, i)
nums[i].append(root.val)
dfs(root.right, i)
nums = [[], []]
dfs(root1, 0)
dfs(root2, 1)
i, j = 0, len(nums[1]) - 1
while i < len(nums[0]) and ~j:
x = nums[0][i] + nums[1][j]
if x == target:
return True
if x < target:
i += 1
else:
j -= 1
return False
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer>[] nums = new List[2];
public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
Arrays.setAll(nums, k -> new ArrayList<>());
dfs(root1, 0);
dfs(root2, 1);
int i = 0, j = nums[1].size() - 1;
while (i < nums[0].size() && j >= 0) {
int x = nums[0].get(i) + nums[1].get(j);
if (x == target) {
return true;
}
if (x < target) {
++i;
} else {
--j;
}
}
return false;
}
private void dfs(TreeNode root, int i) {
if (root == null) {
return;
}
dfs(root.left, i);
nums[i].add(root.val);
dfs(root.right, i);
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool twoSumBSTs(TreeNode* root1, TreeNode* root2, int target) {
vector<int> nums[2];
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
if (!root) {
return;
}
dfs(root->left, i);
nums[i].push_back(root->val);
dfs(root->right, i);
};
dfs(root1, 0);
dfs(root2, 1);
int i = 0, j = nums[1].size() - 1;
while (i < nums[0].size() && j >= 0) {
int x = nums[0][i] + nums[1][j];
if (x == target) {
return true;
}
if (x < target) {
++i;
} else {
--j;
}
}
return false;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func twoSumBSTs(root1 *TreeNode, root2 *TreeNode, target int) bool {
nums := [2][]int{}
var dfs func(*TreeNode, int)
dfs = func(root *TreeNode, i int) {
if root == nil {
return
}
dfs(root.Left, i)
nums[i] = append(nums[i], root.Val)
dfs(root.Right, i)
}
dfs(root1, 0)
dfs(root2, 1)
i, j := 0, len(nums[1])-1
for i < len(nums[0]) && j >= 0 {
x := nums[0][i] + nums[1][j]
if x == target {
return true
}
if x < target {
i++
} else {
j--
}
}
return false
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function twoSumBSTs(root1: TreeNode | null, root2: TreeNode | null, target: number): boolean {
const nums: number[][] = Array(2)
.fill(0)
.map(() => []);
const dfs = (root: TreeNode | null, i: number) => {
if (!root) {
return;
}
dfs(root.left, i);
nums[i].push(root.val);
dfs(root.right, i);
};
dfs(root1, 0);
dfs(root2, 1);
let i = 0;
let j = nums[1].length - 1;
while (i < nums[0].length && j >= 0) {
const x = nums[0][i] + nums[1][j];
if (x === target) {
return true;
}
if (x < target) {
++i;
} else {
--j;
}
}
return false;
}