Monday, August 10, 2020

LeetCode [1214] Two Sum BSTs

1214. Two Sum BSTs
Medium

Given two binary search trees, return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target.

 

Example 1:

Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
Output: true
Explanation: 2 and 3 sum up to 5.

Example 2:

Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
Output: false

 

Constraints:

  • Each tree has at most 5000 nodes.
  • -10^9 <= target, node.val <= 10^9
/**
 * 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 {
    public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
        if(root1==null) return false;
        if(root2==null) return false;
        int s = root1.val+root2.val;
        if(s == target) return true;
        if(s < target){
            return twoSumBSTs(root1.right, root2, target)||twoSumBSTs(root1, root2.right, target);
        }else{
            return twoSumBSTs(root1.left, root2, target)||twoSumBSTs(root1, root2.left, target);
        }
    }
}

No comments:

Post a Comment