53. Two Sum IV - Input is a BST - Jake blog - Java coding practice log (2017)" /> 53. Two Sum IV - Input is a BST - Jake blog - Java coding practice log (2017)" />

653. Two Sum IV – Input is a BST

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.

Example 1:

Input:  5 / \ 3 6 / \ \ 2 4 7 Target = 9  Output:  True  

Example 2:

Input:  5 / \ 3 6 / \ \ 2 4 7 Target = 28  Output:  False

/** * Definition for a binary tree node. * public class TreeNode { * int val; public boolean checkk(TreeNode root, int k,HashSet map){ if(root == null) return false; if(map.contains(k-root.val)) return true; map.add(root.val); return checkk(root.left,k,map) || checkk(root.right,k,map); } }

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:「653. Two Sum IV – Input is a BST」

239
0 0 239

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top