description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def dfs(node, s=set()): if not node: return 0 if target - node.data in s: return 1 s.add(node.data) return dfs(node.left, s) or dfs(node.right, s) return dfs(root, set())
CLASS_DEF FUNC_DEF FUNC_DEF FUNC_CALL VAR IF VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def inorder_iteratively(self, stack, resume_from_mid): while stack: top = stack[-1] if top.left and not resume_from_mid: stack.append(top.left) else: while stack: popped_node = stack.pop() resume_from_mid = True if popped_node.right: stack.append(popped_node.right) resume_from_mid = False return [popped_node.data, stack, resume_from_mid] def backward_inorder_iteratively(self, stack, resume_from_mid): while stack: top = stack[-1] if top.right and not resume_from_mid: stack.append(top.right) else: while stack: popped_node = stack.pop() resume_from_mid = True if popped_node.left: stack.append(popped_node.left) resume_from_mid = False return [popped_node.data, stack, resume_from_mid] def isPairPresent(self, root, target): forward_stack, backward_stack, resume_from_mid = [root], [root], False front, back = self.inorder_iteratively( forward_stack, resume_from_mid ), self.backward_inorder_iteratively(backward_stack, resume_from_mid) while front[0] != back[0]: if front[0] + back[0] == target: return 1 elif front[0] + back[0] > target: back = self.backward_inorder_iteratively(back[1], back[2]) else: front = self.inorder_iteratively(front[1], front[2]) return 0
CLASS_DEF FUNC_DEF WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN LIST VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN LIST VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR LIST VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def until(root, sum1, p): if root == None: return False if until(root.left, sum1, p) == True: return True if sum1 - root.data in p: return True p.add(root.data) return until(root.right, sum1, p) p = set() if until(root, target, p): return 1 else: return 0
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def inorder(self, root, l): if not root: return else: self.inorder(root.left, l) l.append(root.data) self.inorder(root.right, l) return l def solve(self, arr, x): l = 0 r = len(arr) - 1 while l < r: if arr[l] + arr[r] == x: return 1 elif arr[l] + arr[r] < x: l += 1 else: r -= 1 return 0 def isPairPresent(self, root, target): l = [] self.inorder(root, l) return self.solve(l, target)
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): node_values = set() node_queue = [] node_queue.append(root) if root is None: return 0 while len(node_queue) > 0: node = node_queue.pop() if target - node.data in node_values: return 1 else: node_values.add(node.data) if node.left is not None: node_queue.append(node.left) if node.right is not None: node_queue.append(node.right) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, k): s = [] e = [] curr = root rcurr = root def nextleft() -> int: nonlocal curr nonlocal s while curr: s.append(curr) curr = curr.left elmt = s.pop() curr = elmt.right return elmt.data def nextright() -> int: nonlocal rcurr nonlocal e while rcurr: e.append(rcurr) rcurr = rcurr.right elmt = e.pop() rcurr = elmt.left return elmt.data left = nextleft() right = nextright() while left < right: if left + right == k: return 1 if left + right < k: left = nextleft() else: right = nextright() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): s = set() def find(rt, s): if rt: l = find(rt.left, s) if target - rt.data in s: return 1 else: s.add(rt.data) return l or find(rt.right, s) return find(root, s) or 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): res = [] def helper(node): if not node: return helper(node.left) if node.data < target: res.append(node.data) helper(node.right) helper(root) count = 0 left = 0 right = len(res) - 1 while left < right: if res[left] + res[right] < target: left += 1 elif res[left] + res[right] > target: right -= 1 else: count += 1 break return count
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): self.ans = [] def inorder(root): if root == None: return if root.left: inorder(root.left) self.ans.append(root.data) if root.right: inorder(root.right) inorder(root) l = 0 r = len(self.ans) - 1 while l < r: if self.ans[l] + self.ans[r] == target: return 1 if self.ans[l] + self.ans[r] < target: l += 1 if self.ans[l] + self.ans[r] > target: r -= 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): self.ans = 0 self.s1 = set() def check(root): if root == None: return if target - root.data in self.s1: self.ans = 1 self.s1.add(root.data) check(root.left) check(root.right) check(root) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): stack_left = [] stack_right = [] curr_left = root curr_right = root while True: while curr_left: stack_left.append(curr_left) curr_left = curr_left.left while curr_right: stack_right.append(curr_right) curr_right = curr_right.right if not stack_left or not stack_right or stack_left[-1] == stack_right[-1]: break left_val = stack_left[-1].data right_val = stack_right[-1].data if left_val + right_val == target: return 1 elif left_val + right_val < target: curr_left = stack_left.pop().right else: curr_right = stack_right.pop().left return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def trav(root, mp): if root == None: return None mp[root.data] = root.data if root.left: trav(root.left, mp) if root.right: trav(root.right, mp) mp = {} trav(root, mp) def travcheck(root, mp, target): if root == None: return None remaining = target - root.data if remaining in mp: return 1 if ( root.left and travcheck(root.left, mp, target) or root.right and travcheck(root.right, mp, target) ): return 1 return 0 return travcheck(root, mp, target)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): s1, s2 = [], [] node = root while root: s1.append(root) root = root.left while node: s2.append(node) node = node.right while s1[-1].data != s2[-1].data: x, y = s1[-1].data, s2[-1].data if x + y == target: return 1 elif x + y > target: node = s2.pop().left while node: s2.append(node) node = node.right else: node = s1.pop().right while node: s1.append(node) node = node.left return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def inorder(root): nonlocal target, mp, ans if not root: return inorder(root.left) if root.data in mp: ans = 1 else: mp[target - root.data] = 1 inorder(root.right) mp = {} ans = 0 inorder(root) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, k): if root is None: return False stack1 = [] stack2 = [] def dfs(node, stack, isLeft): if node is None: return stack.append(node) if isLeft: dfs(node.left, stack, isLeft) else: dfs(node.right, stack, isLeft) dfs(root, stack1, True) dfs(root, stack2, False) while stack1 and stack2 and stack1[-1] != stack2[-1]: sum_ = stack1[-1].data + stack2[-1].data if sum_ == k: return 1 if sum_ > k: dfs(stack2.pop().left, stack2, False) else: dfs(stack1.pop().right, stack1, True) return 0
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPresent(self, root, target, h): if root is None: return 0 if self.isPresent(root.left, target, h) or self.isPresent( root.right, target, h ): return 1 if target - root.data in h: return 1 else: h.add(root.data) return 0 def isPairPresent(self, root, target): h = set() return self.isPresent(root, target, h)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, node, target): self.map_sum = set() def helper(node, target): if not node: return False if helper(node.left, target): return True diff = target - node.data if diff in self.map_sum: return True self.map_sum.add(node.data) if helper(node.right, target): return True return False return int(helper(node, target))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def moveLeft(self, stk1): n = stk1[-1] stk1.pop() tmp = n.right while tmp: stk1.append(tmp) tmp = tmp.left def moveRight(self, stk2): n = stk2[-1] stk2.pop() tmp = n.left while tmp: stk2.append(tmp) tmp = tmp.right def isPairPresent(self, root, target): stk1 = [] stk2 = [] node = root while node: stk1.append(node) node = node.left node = root while node: stk2.append(node) node = node.right while stk1[-1] != stk2[-1]: if stk1[-1].data + stk2[-1].data == target: return 1 elif stk1[-1].data + stk2[-1].data < target: self.moveLeft(stk1) else: self.moveRight(stk2) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class bstIterator: def addLeft(self, root, st): curr = root while curr: st.append(curr) curr = curr.left def addRight(self, root, st): curr = root while curr: st.append(curr) curr = curr.right def __init__(self, root): self.root = root self.IncrSt = [] self.DecrSt = [] self.addLeft(self.root, self.IncrSt) self.addRight(self.root, self.DecrSt) def IncNext(self): if len(self.IncrSt) > 0: top = self.IncrSt[-1] self.IncrSt.pop() if top.right: self.addLeft(top.right, self.IncrSt) return top.data return None def DecNext(self): if len(self.DecrSt) > 0: top = self.DecrSt[-1] self.DecrSt.pop() if top.left: self.addRight(top.left, self.DecrSt) return top.data return None class Solution: def isPairPresent(self, root, target): iterator = bstIterator(root) while len(iterator.IncrSt) > 0 and len(iterator.DecrSt): if iterator.IncrSt[-1].data + iterator.DecrSt[-1].data == target: return 1 elif iterator.IncrSt[-1].data + iterator.DecrSt[-1].data > target: iterator.DecNext() else: iterator.IncNext() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def __init__(self): self.a1 = {} self.flag = 0 def io(self, a, k): if a: self.io(a.left, k) if k - a.data in self.a1: self.flag = 1 self.a1[a.data] = 1 self.io(a.right, k) def isPairPresent(self, root, target): self.io(root, target) return self.flag
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): stack1 = [] stack2 = [] curr1, curr2 = root, root while True: if curr1 is not None or curr2 is not None: if curr1 is not None: stack1.append(curr1) curr1 = curr1.left if curr2 is not None: stack2.append(curr2) curr2 = curr2.right else: if len(stack1) == 0 or len(stack2) == 0: break left_top = stack1[-1] right_top = stack2[-1] if id(left_top) == id(right_top): break sumval = left_top.data + right_top.data if sumval < target: stack1.pop() curr1 = left_top.right elif sumval > target: stack2.pop() curr2 = right_top.left else: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR WHILE NUMBER IF VAR NONE VAR NONE IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class BSTiterator: def __init__(self, root): self.stackN = [] curr = root while curr: self.stackN.append(curr) curr = curr.left self.stackB = [] while root: self.stackB.append(root) root = root.right def next(self): node = self.stackN.pop() res = node.data node = node.right while node: self.stackN.append(node) node = node.left return res def before(self): node = self.stackB.pop() res = node.data if node.left: node = node.left while node: self.stackB.append(node) node = node.right return res class Solution: def isPairPresent(self, Root, target): it = BSTiterator(Root) l = it.next() r = it.before() while l < r: if l + r < target: l = it.next() elif l + r > target: r = it.before() else: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def __init__(self): self.dp = dict() self.ans = 0 def tr(self, root, target): if root == None: return if self.dp.get(target - root.data) != None: self.ans = int(self.ans or 1) self.dp[root.data] = 1 self.tr(root.left, target) self.tr(root.right, target) def isPairPresent(self, root, target): self.tr(root, target) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF FUNC_CALL VAR BIN_OP VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, r, t): l = [] i = 0 self.df(r, t, l) j = len(l) - 1 while i < j: if l[i] + l[j] == t: return 1 elif l[i] + l[j] < t: i += 1 else: j -= 1 return 0 def df(self, r, t, l): if r: self.df(r.left, t, l) l.append(r.data) self.df(r.right, t, l)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def pushleft(self, st, root): while root: st.append(root) root = root.left def pushright(self, st, root): while root: st.append(root) root = root.right def nextleft(self, st): node = st.pop() self.pushleft(st, node.right) return node.data def nextright(self, st): node = st.pop() self.pushright(st, node.left) return node.data def isPairPresent(self, root, target): stleft = [] stright = [] self.pushleft(stleft, root) self.pushright(stright, root) left, right = self.nextleft(stleft), self.nextright(stright) while left < right: if left + right == target: return 1 if left + right < target: left = self.nextleft(stleft) elif left + right > target: right = self.nextright(stright) return 0
CLASS_DEF FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): self.visited_list = [] self.count = 0 def BS(target, low=0, high=self.count - 1): if low <= high: mid = low + (high - low) // 2 if self.visited_list[mid] > target: return BS(target, low, mid - 1) elif self.visited_list[mid] < target: return BS(target, mid + 1, high) else: return mid return -1 def func(root, target): if root == None: return False lb = func(root.left, target) if BS(target - root.data, 0, self.count - 1) != -1: return True self.visited_list.append(root.data) self.count += 1 rb = func(root.right, target) return lb or rb return 1 if func(root, target) else 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
def inOrder(root): if root == None: return [] else: return inOrder(root.left) + [root.data] + inOrder(root.right) class Solution: def isPairPresent(self, root, target): arr = inOrder(root) l, r = 0, len(arr) - 1 while l < r: summ = arr[l] + arr[r] if summ == target: return 1 elif summ > target: r -= 1 else: l += 1 return 0
FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: d = dict() def inorder(self, root): if root == None: return self.inorder(root.left) self.d[root.data] = 1 self.inorder(root.right) def isPairPresent(self, root, target): self.d = dict() self.inorder(root) for i in self.d: if target - i in self.d: return 1 return 0
CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): arr = self.inOrderTraversal(root) n = len(arr) j = n - 1 for i in range(n): while j > i and arr[i] + arr[j] > target: j -= 1 if i >= j: return 0 if arr[i] + arr[j] == target: return 1 def inOrderTraversal(self, root): if root is None: return [] output = self.inOrderTraversal(root.left) output.append(root.data) output += self.inOrderTraversal(root.right) return output
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): my_list = [] self._in_order_travers(root, my_list) _index = None _target_sum_found = 0 for _ in range(len(my_list)): _to_find = target - my_list[_] _is_found = self._bs(my_list, 0, len(my_list) - 1, _to_find, _index) if _is_found and _index != _: _target_sum_found = 1 break return _target_sum_found def _bs(self, my_list, low, high, value, _index): _middle = (low + high) // 2 if my_list[_middle] == value: _index = _middle return True while low <= high: if value > my_list[_middle]: return self._bs(my_list, _middle + 1, high, value, _index) else: return self._bs(my_list, low, _middle - 1, value, _index) return False def _in_order_travers(self, root, res): if root: self._in_order_travers(root.left, res) res.append(root.data) self._in_order_travers(root.right, res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER WHILE VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class InorderGenerator: def __init__(self, root): self.root = root self.cur = root self.stack = [] def getNext(self): while self.cur: self.stack.append(self.cur) self.cur = self.cur.left if self.stack: lastNode = self.stack.pop() self.cur = lastNode.right return lastNode.data else: return None class R_InorderGenerator: def __init__(self, root): self.root = root self.cur = root self.stack = [] def getNext(self): while self.cur: self.stack.append(self.cur) self.cur = self.cur.right if self.stack: lastNode = self.stack.pop() self.cur = lastNode.left return lastNode.data else: return None class Solution: def isPairPresent(self, root, target): inorder = InorderGenerator(root) rinorder = R_InorderGenerator(root) left = inorder.getNext() right = rinorder.getNext() while left is not None and right is not None: if left == right: return 0 if left + right == target: return 1 elif left + right > target: right = rinorder.getNext() else: left = inorder.getNext() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NONE VAR NONE IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, k): self.inOrder = [] self.inorder(root) l, r = 0, len(self.inOrder) - 1 while l < r: target = self.inOrder[l] + self.inOrder[r] if target == k: return 1 elif target > k: r -= 1 else: l += 1 return 0 def inorder(self, root): if root is None: return None self.inorder(root.left) self.inOrder.append(root.data) self.inorder(root.right) class BSTIterator: def __init__(self, root, isReverse): self.stack = [] self.reverse = True self.reverse = isReverse self.pushAll(root) def next(self): curr = self.stack.pop() if self.reverse == False: self.pushAll(curr.right) else: self.pushAll(curr.left) return curr.data def pushAll(self, root): while root: self.stack.append(root) if self.reverse == True: root = root.right else: root = root.left class Solution: def isPairPresent(self, root, k): if root is None: return 0 l = BSTIterator(root, False) r = BSTIterator(root, True) i, j = l.next(), r.next() while i < j: target = i + j if target == k: return 1 elif target < k: i = l.next() else: j = r.next() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): dic = {} ans = False def iterate(root, target): nonlocal dic, ans if root is None: return if target - root.data in dic: ans = True else: dic[root.data] = 1 iterate(root.left, target) iterate(root.right, target) iterate(root, target) return 1 if ans else 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def check(self, root, k, d): if not root: return 0 if self.check(root.left, k, d): return 1 if k - root.data not in d: d[root.data] = 1 else: return 1 return self.check(root.right, k, d) def isPairPresent(self, root, target): d = {} return self.check(root, target, d)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): arr = [] self.in_order(root, arr) i = 0 j = len(arr) - 1 while i < j: if arr[i] + arr[j] == target: return 1 if arr[i] + arr[j] < target: i += 1 if arr[i] + arr[j] > target: j -= 1 return 0 def in_order(self, root, arr): if root is None: return self.in_order(root.left, arr) arr.append(root.data) self.in_order(root.right, arr)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def search(self, root, n): if root is None: return self.search(root.left, n) n.append(root.data) self.search(root.right, n) def isPairPresent(self, root, target): n = [] self.search(root, n) i = 0 j = len(n) - 1 s = 0 while i < j: s = n[i] + n[j] if s == target: return 1 if s < target: i += 1 if s > target: j -= 1 return 0
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class InorderGenerator: def __init__(self, root): self.root = root self.cur = root self.stack = [] def getNext(self): while self.cur: self.stack.append(self.cur) self.cur = self.cur.left if self.stack: lastNode = self.stack.pop() self.cur = lastNode.right return lastNode.data else: return None class R_InorderGenerator: def __init__(self, root): self.root = root self.cur = root self.stack = [] def getNext(self): while self.cur: self.stack.append(self.cur) self.cur = self.cur.right if self.stack: lastNode = self.stack.pop() self.cur = lastNode.left return lastNode.data else: return None class Solution: def isPairPresent(self, root, target): inorder = InorderGenerator(root) rinorder = R_InorderGenerator(root) left = inorder.getNext() right = rinorder.getNext() while left is not None and right is not None: if left == right: return 0 if left + right == target: return 1 elif left + right > target: right = rinorder.getNext() else: left = inorder.getNext() return 0 def inorderGenerator(root): if root: yield from inorderGenerator(root.left) yield root.data yield from inorderGenerator(root.right) def rinorderGenerator(root): if root: yield from rinorderGenerator(root.right) yield root.data yield from rinorderGenerator(root.left) class Solution1: def isPairPresent(self, root, target): inorder = inorderGenerator(root) rinorder = rinorderGenerator(root) left = next(inorder) right = next(rinorder) while left is not None and right is not None: if left == right: return 0 if left + right == target: return 1 if left + right > target: right = next(rinorder) else: left = next(inorder) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NONE VAR NONE IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN NUMBER
Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in O(n). Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
class Solution: def productExceptSelf(self, nums): nums0 = nums.copy() son = 1 if 0 in nums0: nums0.remove(0) for i in nums0: son *= i pro = 1 for i in nums: pro *= i result = [] for i in nums: if i == 0: result.append(son) else: result.append(pro // i) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in O(n). Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
class Solution: def productExceptSelf(self, nums): p = 1 result = [] for i in range(0, len(nums)): result.append(p) p = p * nums[i] p = 1 for i in range(len(nums) - 1, -1, -1): result[i] = result[i] * p p = p * nums[i] return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: days = [(1 if hour > 8 else -1) for hour in hours] prefSums = days[:] for i in range(1, len(hours)): prefSums[i] = days[i] + prefSums[i - 1] result = 0 prefixLookup = {} for i in range(len(hours)): sumToHere = prefSums[i] if sumToHere >= 1: result = max(result, i + 1) else: earlierPrefSumNeeded = sumToHere - 1 if earlierPrefSumNeeded in prefixLookup: result = max(result, i - prefixLookup[earlierPrefSumNeeded]) if sumToHere not in prefixLookup: prefixLookup[sumToHere] = i return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: score = 0 dic = {} result = 0 for i, hour in enumerate(hours): if hour > 8: score += 1 else: score -= 1 if score > 0: result = i + 1 if score not in dic: dic[score] = i if score - 1 in dic: result = max(result, i - dic[score - 1]) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: seen = {} result, score = 0, 0 for i, num in enumerate(hours): score += 1 if num > 8 else -1 if score > 0: result = i + 1 elif score - 1 in seen: result = max(result, i - seen[score - 1]) seen.setdefault(score, i) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours): res = score = 0 seen = {} for i, h in enumerate(hours): print((i, h)) if h > 8: score += 1 else: score -= 1 if score > 0: res = i + 1 seen.setdefault(score, i) if score - 1 in seen: res = max(res, i - seen[score - 1]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: stack = [] ans = accu = 0 for i, h in enumerate(hours): if h > 8: accu += 1 else: accu -= 1 if accu > 0: ans = i + 1 else: L, U = -1, len(stack) while L + 1 < U: m = (L + U) // 2 if stack[m][1] < accu: U = m else: L = m if U < len(stack): ans = max(ans, i - stack[U][0]) if not stack or stack[-1][1] > accu: stack.append((i, accu)) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: n = len(hours) hours = [(1 if x > 8 else -1) for x in hours] s = 0 idx = {} ans = 0 for i in range(len(hours)): s += hours[i] if s > 0: ans = i + 1 if s not in idx: idx[s] = i if s - 1 in idx: ans = max(ans, i - idx[s - 1]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: lt = [] for hour in hours: if hour > 8: lt.append(1) else: lt.append(-1) seen = {} s = 0 mx = 0 for idx, ele in enumerate(lt): s += ele print((s, idx, mx)) if s > 0: mx = max(mx, idx + 1) elif s - 1 in seen: i = seen[s - 1] mx = max(mx, idx - i) if s not in seen: seen[s] = idx return mx
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: seen = dict() cnt = 0 res = 0 for idx, h in enumerate(hours): if h > 8: cnt += 1 else: cnt -= 1 if cnt > 0: res = idx + 1 elif cnt - 1 in seen: res = max(res, idx - seen[cnt - 1]) if cnt not in seen: seen[cnt] = idx return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hoursLen = len(hours) tDay = [] preSum = 0 preSumDict = {(0): -1} preSumKeys = [0] ans = 0 def insertAt(x): l = 0 r = len(preSumKeys) - 1 if x < preSumKeys[l]: return 0 if x > preSumKeys[r]: return r + 1 while r > l: m = (r + l) // 2 if preSumKeys[m] < x: l = m + 1 else: r = m return l ans = 0 for i, hour in enumerate(hours): if hour > 8: tDay.append(1) preSum += 1 else: tDay.append(-1) preSum -= 1 tempIndex = insertAt(preSum) if preSum not in preSumDict: preSumDict[preSum] = i preSumKeys.insert(tempIndex, preSum) if tempIndex > 0: preSumDict[preSumKeys[tempIndex]] = min( preSumDict[preSumKeys[tempIndex - 1]], preSumDict[preSumKeys[tempIndex]], ) ans = max(ans, i - preSumDict[preSumKeys[tempIndex - 1]]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: s = [] for i in hours: if i > 8: s.append(1) else: s.append(-1) n = len(s) presum = [0] * (n + 1) for i in range(1, n + 1): presum[i] = presum[i - 1] + s[i - 1] stack = [] n = len(presum) for i in range(n): if not stack or presum[stack[-1]] > presum[i]: stack.append(i) res = 0 i = n - 1 while i > res: while stack and presum[stack[-1]] < presum[i]: res = max(res, i - stack[-1]) stack.pop() i -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: prefix = [] for hour in hours: if hour > 8: prefix.append(1) else: prefix.append(-1) curr_sum = 0 seen = {(0): -1} res = 0 for i, num in enumerate(prefix): curr_sum += num if curr_sum > 0: res = i + 1 if curr_sum - 1 in seen: res = max(res, i - seen[curr_sum - 1]) if curr_sum not in seen: seen[curr_sum] = i return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: score, longest = 0, 0 index = {} for i, n in enumerate(hours): score += 1 if n > 8 else -1 if score > 0: longest = max(longest, i + 1) index.setdefault(score, i) if score - 1 in index: longest = max(longest, i - index[score - 1]) return longest
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: final = 0 score = 0 stack = {} for i in range(len(hours)): if hours[i] > 8: score += 1 else: score -= 1 stack.setdefault(score, i) if score > 0: final = i + 1 if score - 1 in stack: final = max(final, i - stack[score - 1]) return final
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: val = 0 result = 0 table = dict() for i in range(len(hours)): hour = hours[i] if hour > 8: val += 1 else: val -= 1 if val > 0: result = i + 1 else: if val not in table: table[val] = i if val - 1 in table: dist = i - table[val - 1] if dist > result: result = dist return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [(1 if i > 8 else -1) for i in hours] hsh = {} vals = [(0) for i in hours] sm = 0 for i in range(len(hours) - 1, -1, -1): sm += hours[i] if sm > 0: vals[i] = max(vals[i], len(hours) - i) hours[i] = sm smo = hours[i] - 1 if smo in hsh: v = hsh[smo] vals[i] = max(vals[i], v - i + vals[v]) if hours[i] not in hsh: hsh[hours[i]] = i return max(vals)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: bits = [(1 if x > 8 else -1) for x in hours] pref = [0] * (len(bits) + 1) for i in range(1, len(bits) + 1): pref[i] = pref[i - 1] + bits[i - 1] ans = 0 min_val = len(bits) + 1 for y in sorted(enumerate(pref), key=lambda x: (x[1], -x[0])): min_val = min(min_val, y[0]) ans = max(ans, y[0] - min_val) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, h: List[int]) -> int: n = len(h) for i in range(n): if h[i] > 8: h[i] = 1 else: h[i] = -1 pre = [0] * n pre[0] = h[0] maxi = 0 for i in range(1, n): pre[i] = pre[i - 1] + h[i] d = {} print(pre) for x in range(n): if pre[x] > 0: maxi = x + 1 if d.get(pre[x] - 1, -1) != -1: maxi = max(maxi, x - d[pre[x] - 1]) if d.get(pre[x], -1) == -1: d[pre[x]] = x return maxi
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: i = 0 prefixSum = 0 existed = {(0): 0} res = 0 while i <= len(hours) - 1: if hours[i] >= 9: prefixSum += 1 else: prefixSum -= 1 if prefixSum not in existed: existed[prefixSum] = i + 1 if prefixSum - 1 in existed: res = max(res, i + 1 - existed[prefixSum - 1]) if prefixSum >= 1: res = max(res, i + 1) i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: pre_sum = [0] for i, v in enumerate(hours): hours[i] = 1 if v > 8 else -1 pre_sum.append(pre_sum[-1] + hours[i]) dec_stack = [] for i, v in enumerate(pre_sum): if len(dec_stack) == 0 or len(dec_stack) > 0 and v < pre_sum[dec_stack[-1]]: dec_stack.append(i) max_r = -math.inf max_width = 0 for r in range(len(pre_sum) - 1, -1, -1): n = pre_sum[r] if n < max_r: continue max_r = n while len(dec_stack) > 0: left_idx = dec_stack[-1] if pre_sum[left_idx] < n: dec_stack = dec_stack[0:-1] max_width = max(max_width, r - left_idx) else: break if len(dec_stack) == 0: break return max_width
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: nums = [] for hour in hours: if hour > 8: nums.append(1) else: nums.append(-1) prevsum, sumv = [], 0 for num in nums: sumv += num prevsum.append(sumv) stack = [] prevsum = [0] + prevsum for i, val in enumerate(prevsum): if not stack or prevsum[stack[-1]] > val: stack.append(i) i, res = len(prevsum) - 1, 0 while i >= 0: while stack and prevsum[stack[-1]] < prevsum[i]: res = max(res, i - stack[-1]) stack.pop() i -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: d = {} P = [0] F = [(0) for i in range(len(hours))] res = 0 for i in range(len(hours)): P.append(P[-1] + (1 if hours[i] > 8 else -1)) if P[-1] > 0: F[i] = i + 1 else: s = d.get(P[-1] - 1, -1) if s >= 0: F[i] = max(F[i], i - s + F[s]) if P[-1] not in d: d[P[-1]] = i res = max(res, F[i]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: s = 0 maxLen = 0 seen = {} for idx, hour in enumerate(hours): if hour > 8: s += 1 else: s -= 1 if s > 0: res = idx + 1 maxLen = max(maxLen, res) elif s - 1 in seen: res = idx - seen[s - 1] maxLen = max(maxLen, res) if s not in seen: seen[s] = idx return maxLen
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def findslavedays(self, hours): a = 0 b = 0 slave = 0 nonslave = 0 slavedays = 0 while b < len(hours): if slave > nonslave: slavedays = max(slavedays, b - a + slave - nonslave - 1) if a == b: if hours[a] <= 8: nonslave += 1 else: slave += 1 b += 1 continue if slave < nonslave: if hours[a] <= 8: nonslave -= 1 else: slave -= 1 a += 1 elif slave >= nonslave: if hours[b] <= 8: nonslave += 1 else: slave += 1 b += 1 if slave > nonslave: slavedays = max(slavedays, b - a + slave - nonslave - 1) print(a, b) while a > 0: a -= 1 if hours[a] <= 8: nonslave += 1 else: slave += 1 if slave > nonslave: slavedays = max(slavedays, b - a + slave - nonslave - 1) if slave > nonslave: slavedays = max(slavedays, b - a + slave - nonslave - 1) return slavedays def longestWPI(self, hours: List[int]) -> int: isslave = [-1] * len(hours) sumslave = 0 for i in range(len(isslave)): if hours[i] > 8: isslave[i] = 1 dic = {} maxslavedays = 0 print(isslave) for i in range(len(isslave)): sumslave = isslave[i] + sumslave if sumslave > 0: maxslavedays = max(maxslavedays, i + 1) if sumslave not in dic: dic[sumslave] = i if sumslave - 1 in dic: maxslavedays = max(maxslavedays, i - dic[sumslave - 1]) print(dic) return maxslavedays print(isslave, sumslave) a = -1 b = 0 dic = {} dic[0] = -1 for i in range(len(isslave)): if sumslave[i] > 0: b = i maxslavedays = max(maxslavedays, b - a) if sumslave[i] <= 0: a = i return maxslavedays return 0 slavelength1 = self.findslavedays(hours) slavelength2 = self.findslavedays(hours[::-1]) return max(slavelength1, slavelength2)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: L = [(1 if i > 8 else -1) for i in hours] K = [(1 if i > 8 else -1) for i in hours] mp = {} for i in range(0, len(hours)): if i > 0: s = L[i] = L[i] + L[i - 1] else: s = L[i] if s not in mp: mp[s] = {i: True} else: mp[s][i] = True result = 0 for i, c in enumerate(L): if L[-1] > L[i]: result = max(result, len(K) - i - 1) if 0 < L[i]: result = max(result, i + 1) if i > 0 and L[i - 1] + 1 in mp: s = mp[L[i - 1] + 1] result = max(result, max(s) - i + 1) if L[i] - 1 in mp: s = mp[L[i] - 1] result = max(result, i - min(s)) return result def longestWPI(self, hours: List[int]) -> int: mp = {} accu = 0 result = 0 for i, v in enumerate(hours): accu += 1 if v > 8 else -1 if accu > 0: result = max(result, i + 1) print((result, i)) if accu - 1 in mp: result = max(result, i - mp[accu - 1]) print((result, i)) if accu not in mp: mp[accu] = i return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: d = {} d[0] = -1 res = 0 for i in range(len(hours)): hours[i] = (1 if hours[i] > 8 else -1) + (0 if i == 0 else hours[i - 1]) if hours[i] not in d: d[hours[i]] = i if hours[i] > 0: res = i + 1 target = hours[i] - 1 if target in d: res = max(res, i - d[target]) print(hours) print(d) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [(1 if h > 8 else -1) for h in hours] down = {(1): 0} s = 0 ans = 0 for i, h in enumerate(hours): if h < 0: if s not in down: down[s] = i s += h if s in down: ans = max(ans, i - down[s]) if s > 0: ans = max(ans, i + 1) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): if root is None: return 0 leftSubTree = 0 rightSubTree = 0 current = 0 if low < root.data: leftSubTree = self.getCount(root.left, low, high) if low <= root.data <= high: current += 1 if high >= root.data: rightSubTree = self.getCount(root.right, low, high) return leftSubTree + rightSubTree + current
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def solve(self, root, low, high): if root is None: return if low <= root.data <= high: self.cnt += 1 self.solve(root.left, low, high) self.solve(root.right, low, high) def getCount(self, root, low, high): self.cnt = 0 self.solve(root, low, high) return self.cnt
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def traverse(root): if root is None: return [] return traverse(root.left) + [root.data] + traverse(root.right) arr = traverse(root) countOfNodes = 0 for i in range(len(arr)): if arr[i] >= low and arr[i] <= high: countOfNodes += 1 return countOfNodes
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): self.total = 0 def f(root): if not root: return if root.data <= high and root.data >= low: self.total += 1 f(root.left) f(root.right) return elif root.data < low: f(root.right) elif root.data > high: f(root.left) else: return f(root) return self.total
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def count(root, cnt): if root == None: return if root.data > high: count(root.left, cnt) elif root.data < low: count(root.right, cnt) else: cnt[0] += 1 count(root.left, cnt) count(root.right, cnt) cnt = [0] count(root, cnt) return cnt[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): if root is None: return 0 output = low <= root.data <= high output += self.getCount(root.left, low, high) output += self.getCount(root.right, low, high) return output
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getTrav(self, root, low, high, li): if root: if root.data >= low and root.data <= high: li[0] += 1 self.getTrav(root.left, low, high, li) self.getTrav(root.right, low, high, li) def getCount(self, root, low, high): li = [0] if root: self.getTrav(root, low, high, li) return li[0] else: return li[0]
CLASS_DEF FUNC_DEF IF VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): re = [] def trav(rot): re.append(rot.data) if rot.left is not None: trav(rot.left) if rot.right is not None: trav(rot.right) trav(root) re = sorted(re) ans = 0 for i in re: if low <= i <= high: ans += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def solve(self, root, low, high, ans): if root == None: return if root.data >= low and root.data <= high: ans.append(root.data) self.solve(root.left, low, high, ans) self.solve(root.right, low, high, ans) def getCount(self, root, low, high): ans = [] self.solve(root, low, high, ans) return len(ans)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): if root == None: return 0 l = self.getCount(root.left, low, high) r = self.getCount(root.right, low, high) if low <= root.data <= high: return l + r + 1 else: return l + r
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): s = [] cur = root while cur: s.append(cur) cur = cur.left res = 0 while s: top = s.pop() if low <= top.data <= high: res += 1 cur = top.right while cur: s.append(cur) cur = cur.left return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def count(self, root, l, h): if root is None: return self.count(root.left, l, h) if root.data >= l and root.data <= h: self.c += 1 self.count(root.right, l, h) def getCount(self, root, low, high): self.c = 0 self.count(root, low, high) return self.c
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def foo(root, low, high, res): if not root: return foo(root.left, low, high, res) if root.data >= low and root.data <= high: res[0] += 1 foo(root.right, low, high, res) res = [0] foo(root, low, high, res) return res[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): return self._getcount(root, low, high) def _getcount(self, node, low, high): if not node: return 0 left_count = self._getcount(node.left, low, high) right_count = self._getcount(node.right, low, high) if low <= node.data <= high: return 1 + left_count + right_count else: return left_count + right_count
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): queue = [root] res = [] while queue: level = [] val = [] for i in queue: if i == None: continue val.append(i.data) level.append(i.left) level.append(i.right) if val: res += val queue = level count = 0 for i in res: if i >= low and i <= high: count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): q = [] c = 0 q.append(root) while q: z = q.pop(0) if low <= z.data <= high: c += 1 if z.left is not None: q.append(z.left) if z.right is not None: q.append(z.right) return c
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def find(node, ino): if node: Solution.find(node.left, ino) ino.append(node.data) Solution.find(node.right, ino) else: return def getCount(self, root, low, high): ino = [] Solution.find(root, ino) cnt = 0 for val in ino: if val >= low and val <= high: cnt += 1 return cnt
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def __init__(self): self.ans = 0 def getCount(self, root, low, high): def inorder(root, low, high): if root: inorder(root.left, low, high) if root.data <= high and root.data >= low: self.ans += 1 inorder(root.right, low, high) inorder(root, low, high) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): l = [0] def f(t): if t == None: return f(t.left) if t.data >= low and t.data <= high: l[0] += 1 f(t.right) f(root) return l[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): if root == None: return 0 lsize = 0 rsize = 0 if root.data > low: lsize = self.getCount(root.left, low, high) if root.data < high: rsize = self.getCount(root.right, low, high) if root.data >= low and root.data <= high: return rsize + lsize + 1 else: return rsize + lsize
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): a = [] c = 0 def inorder(root): if root is None: return inorder(root.left) a.append(root.data) inorder(root.right) inorder(root) for i in range(len(a)): if a[i] >= low and a[i] <= high: c += 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def ranges(root, low, high): if not root: return 0 if root.data in range(low, high + 1): l = ranges(root.left, low, high) r = ranges(root.right, low, high) return l + r + 1 elif root.data > high: return ranges(root.left, low, high) else: return ranges(root.right, low, high) return ranges(root, low, high)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): if root == None: return 0 if root.data >= low and root.data <= high: return ( 1 + self.getCount(root.left, low, high) + self.getCount(root.right, low, high) ) elif root.data < low: return self.getCount(root.right, low, high) elif root.data > high: return self.getCount(root.left, low, high)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def dfs(root, l, h): nonlocal c if root.data >= l and root.data <= h: c = c + 1 if root.left != None: dfs(root.left, l, h) if root.right != None: dfs(root.right, l, h) c = 0 dfs(root, low, high) return c
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): count = 0 def dfs(root): if not root: return if low <= root.data <= high: nonlocal count count += 1 if root.data >= low: dfs(root.left) if root.data <= high: dfs(root.right) dfs(root) return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): return self._getCount(root, low, high) def _getCount(self, node, l, h) -> int: if not node: return 0 if l <= node.data <= h: return ( 1 + self._getCount(node.left, l, h) + self._getCount(node.right, l, h) ) else: return self._getCount(node.left, l, h) + self._getCount(node.right, l, h)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
def inorder(root): if not root: return [] return inorder(root.left) + [root.data] + inorder(root.right) class Solution: def getCount(self, root, low, high): a = inorder(root) c = [] for i in a: if i >= low and i <= high: c.append(i) return len(c)
FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): a = [] def fun(root, l, h): if root: if root.data >= l and root.data <= h: a.append(root.data) fun(root.left, l, h) fun(root.right, l, h) fun(root, low, high) return len(a)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
def func(root, l, h, ans): if root is None: return if l <= root.data <= h: ans[0] += 1 if root.data > l: func(root.left, l, h, ans) if root.data < h: func(root.right, l, h, ans) class Solution: def getCount(self, root, low, high): ans = [0] func(root, low, high, ans) return ans[0]
FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): def dfs(root, count, l, h): if not root: return 0 left = dfs(root.left, count, l, h) right = dfs(root.right, count, l, h) if root.data >= l and root.data <= r: count += 1 return count + left + right return dfs(root, 0, low, high)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def Inorder(self, root, low, high, count): if root == None: return None else: self.Inorder(root.left, low, high, count) if low <= root.data <= high: count.append(root.data) self.Inorder(root.right, low, high, count) def getCount(self, root, low, high): count = [] self.Inorder(root, low, high, count) return len(count)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
def getAns(root, low, high): global ans if root == None or high < low: return if root.data >= low and root.data <= high: ans += 1 getAns(root.left, low, root.data - 1) getAns(root.right, root.data, high) elif root.data < low: getAns(root.right, low, high) elif root.data > high: getAns(root.left, low, high) class Solution: def getCount(self, root, low, high): global ans ans = 0 getAns(root, low, high) return ans
FUNC_DEF IF VAR NONE VAR VAR RETURN IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): ans = 0 if root.left: ans += self.getCount(root.left, low, high) if root.right: ans += self.getCount(root.right, low, high) if low <= root.data <= high: ans += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def getCount(self, root, low, high): vals = [] def inorder(root): if root == None: return nonlocal vals inorder(root.left) vals.append(root.data) inorder(root.right) return vals inorder(root) count = 0 for i in vals: if i < low: continue if i >= low and i <= high: count += 1 if i > high: return count return count
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR
Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. The values smaller than root go to the left side The values greater and equal to the root go to the right side Example 1: Input: 10 / \ 5 50 / / \ 1 40 100 l = 5, h = 45 Output: 3 Explanation: 5 10 40 are the node in the range Example 2: Input: 5 / \ 4 6 / \ 3 7 l = 2, h = 8 Output: 5 Explanation: All the nodes are in the given range. Your Task: This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count. Expected Time Complexity: O(N) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 100 1 <= l < h < 10^{3}
class Solution: def inorder(self, node, arr): if node is None: return arr self.inorder(node.left, arr) arr.append(node.data) self.inorder(node.right, arr) return arr def getCount(self, root, low, high): arr = self.inorder(root, []) counter = 0 for i in arr: if i <= high and i >= low: counter = counter + 1 return counter
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR