You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Solution - 1 Recursion - InOrderTraversal Two Pointers
classA {
// Runtime: 544 ms, faster than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.// Memory Usage: 150 MB, less than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.List<int> array = [];
// inOrder to reading the tree top to bottomvoidinorder(TreeNode? root) {
// if our root is null means there is nothing to begin withif (root ==null) return;
// reading the tree from left sideinorder(root.left);
// adding the value inside the array
array.add(root.val);
// than reading the tree from left sideinorder(root.right);
}
boolfindTarget(TreeNode? root, int k) {
// if the root is null means we have found nothingif (root ==null) returnfalse;
// than simply we will return the root of the treeinorder(root);
// first pointer - beginning of the array (Left Side)int low =0;
// second pointer - end of the array (Right Side)int high = array.length -1;
// assuming all the value on the left side means low side of the array is smaller than the right sidewhile (low < high) {
// if the sum of the first value and the last value from both side is same as target than turn trueif (array[low] + array[high] == k)
returntrue;
// if sum is less than the target value means the target value is on right side of the arrayelseif (array[low] + array[high] < k)
// than we will move forward toward the right side
low++;
else// if sum is greater than the target value means the target value is on right side of the array// than we will move backward toward the right side
high--;
}
// if everything sucks than we will return falsereturnfalse;
}
}
Solution - 2 Using Queue Level Order Traversal with Sorting
classSolution {
// Runtime: 689 ms, faster than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.// Memory Usage: 159.9 MB, less than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.boolfindTarget(TreeNode? root, int k) {
// if the root is null means we have nothingif (root ==null) returnfalse;
// to sort our treeList<int> array = [];
// looking for target - extra variableint target = k;
//level order traversal and storing values in listQueue<TreeNode?> queue =Queue();
// adding our root to the queue
queue.add(root);
// assuming nothing is emptywhile (queue.isNotEmpty) {
// if so we will remove the first valueTreeNode tempNode = queue.removeFirst()!;
// than we will add the value into the array for sorting - means arranging the values
array.add(tempNode.val);
// if the left side have values and it's not nullif (tempNode.left !=null) {
// we will add all the value into the queue
queue.add(tempNode.left);
}
// if the right side is not null - means not empty than we will also add themif (tempNode.right !=null) {
queue.add(tempNode.right);
}
}
// if our array is only have one value or 0 means less than blah blah we have nothingif (array.length <=1) returnfalse;
//simple two sum logic// lopping through each and every elementfor (int i =0; i < array.length; i++) {
// the temp value if the target value - the each and every elementint temp = target - array.elementAt(i);
// if our array contain that valueif (array.contains(temp)) {
// and it matches with array value at any pointint pos = array.indexOf(temp);
// if the element and the position matches Congratulation -*You are Married*-if (i == pos) continue;
returntrue;
}
}
// otherwise she Rejected your proposal -* Better Luck Next Time Bro*-returnfalse;
}
}
Solution - 3 Recursive with HASHSET
classSolution {
// Runtime: 571 ms, faster than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.// Memory Usage: 153.4 MB, less than 100.00% of Dart online submissions for Two Sum IV - Input is a BST.HashSet hashSet =HashSet();
boolfindTarget(TreeNode? root, int k) {
if (root ==null) returnfalse;
//Now k= num1 + num2 then here num1 is data and num2 is root.valint data = k - root.val;
if (hashSet.contains(data)) {
//Check the data is contain setreturntrue;
} else {
hashSet.add(root.val);
}
returnfindTarget(root.left, k) ?true:findTarget(root.right, k);
}
}