description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle. Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7. Example 1: Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: As illustrated in the picture. Example 2: Input: [[0,0,1000000000,1000000000]] Output: 49 Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49. Note: 1 <= rectangles.length <= 200 rectanges[i].length = 4 0 <= rectangles[i][j] <= 10^9 The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.
class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: x_set = set() points = [] for x1, y1, x2, y2 in rectangles: x_set.add(x1) x_set.add(x2) heapq.heappush(points, (y1, x1, x2, 1)) heapq.heappush(points, (y2, x1, x2, -1)) x_list = sorted(list(x_set)) mapping = {x: i for i, x in enumerate(x_list)} count = [0] * len(x_list) res = 0 pre_y = points[0][0] while points: cur_y = points[0][0] h = cur_y - pre_y for i in range(len(x_list) - 1): if count[i] > 0: res += (x_list[i + 1] - x_list[i]) * h res %= 10**9 + 7 while points and points[0][0] == cur_y: cur_y, x1, x2, cnt = heapq.heappop(points) for i in range(mapping[x1], mapping[x2]): count[i] += cnt pre_y = cur_y return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): arr = [] def inorder(root, arr): if root == None: return inorder(root.left, arr) arr.append(root.data) inorder(root.right, arr) inorder(root, arr) c = float("inf") for i in range(len(arr)): c = min(abs(arr[i] - K), c) return c
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): min_diff = float("inf") def dfs(node): nonlocal min_diff if not node: return diff = abs(node.data - K) if diff < min_diff: min_diff = diff if K < node.data: dfs(node.left) else: dfs(node.right) dfs(root) return min_diff
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K, mi=999999): if root is None: return mi if mi > abs(root.data - K): mi = abs(root.data - K) if K < root.data: mi = self.minDiff(root.left, K, mi) else: mi = self.minDiff(root.right, K, mi) return mi
CLASS_DEF FUNC_DEF NUMBER IF VAR NONE RETURN VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def helper(self, node, k): if not node: return self.res = min(self.res, abs(node.data - k)) self.helper(node.left, k) self.helper(node.right, k) def minDiff(self, root, K): self.res = float("inf") self.helper(root, K) return self.res
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): maxi = [] def inorder(root): if root != None: inorder(root.left) maxi.append(root.data) inorder(root.right) inorder(root) t = [abs(K - i) for i in maxi] return min(t)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): closee = root.data stack = [root] while stack: node = stack.pop() if abs(node.data - k) < abs(closee - k): closee = node.data if node.left and k < node.data: stack.append(node.left) elif node.right and k > node.data: stack.append(node.right) return abs(closee - k)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: class Node: def __init__(self, data): self.right = None self.left = None self.data = data res = [] def inorder(self, root, K, res): if root is None: return True self.inorder(root.left, K, res) res.append(abs(root.data - K)) self.inorder(root.right, K, res) def minDiff(self, root, K): res = [] self.inorder(root, K, res) return min(res)
CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def abs(self, k: int) -> int: if k < 0: return -1 * k return k def minDiffHelper(self, root, K, leastDiffSoFar): if not root: return leastDiffSoFar if self.abs(root.data - K) < leastDiffSoFar: leastDiffSoFar = self.abs(root.data - K) if K > root.data: return self.minDiffHelper(root.right, K, leastDiffSoFar) return self.minDiffHelper(root.left, K, leastDiffSoFar) def minDiff(self, root, K): return self.minDiffHelper(root, K, 10000000000)
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN BIN_OP NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): if not root: return def min1(root): min2 = root.data if root.left: min2 = min1(root.left) return min2 def max1(root): max2 = root.data if root.right: max2 = max1(root.right) return max2 def help(root, K): if not root: return val = abs(K - root.data) if root.right: if K > root.data and abs(K - min1(root.right)) <= val: val = help(root.right, K) if root.left: if K <= root.data and abs(K - max1(root.left)) <= val: val = help(root.left, K) if root.right and root.left: if K >= root.data and abs(K - min1(root.right)) >= val: if abs(K - max1(root.left)) <= val: val = help(root.left, K) elif K <= root.data and abs(K - max1(root.left)) >= val: if abs(K - min1(root.right)) <= val: val = help(root.left, K) return val return help(root, K)
CLASS_DEF FUNC_DEF IF VAR RETURN FUNC_DEF ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = [10**9] self.util(root, K, ans) return ans[0] def util(self, root, k, ans): if not root: return self.util(root.left, k, ans) if ans[0] > abs(k - root.data): ans[0] = abs(k - root.data) self.util(root.right, k, ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, k): re = [] def trav(s): re.append(s.data) if s.left is not None: trav(s.left) if s.right is not None: trav(s.right) trav(root) m = abs(k - re[0]) for i in re: m = min(m, abs(k - i)) return m
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 BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): if not root: return float("inf") if root.data == K: return 0 if root.data > K: return min(abs(root.data - K), self.minDiff(root.left, K)) return min(abs(root.data - K), self.minDiff(root.right, K))
CLASS_DEF FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: def minDiff(self, root, K): def check(root): nonlocal res if not root: return sys.maxsize if root.data == K: res = 0 if K < root.data: res = min(abs(root.data - K), res) check(root.left) else: res = min(abs(root.data - K), res) check(root.right) res = sys.maxsize check(root) return res
IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: def minDiff(self, root, K): ans = sys.maxsize while root: if root.data == K: return 0 elif root.data > K: ans = min(ans, root.data - K) root = root.left else: ans = min(ans, K - root.data) root = root.right return ans
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): temp = root closest = abs(root.data - K) while temp: if abs(temp.data - K) < closest: closest = abs(temp.data - K) if temp.data < K: temp = temp.right elif temp.data > K: temp = temp.left else: return 0 return closest
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
def solve(root, k, ans): if root is None: return else: ans.data = min(ans.data, abs(root.data - k)) solve(root.left, k, ans) solve(root.right, k, ans) class Ans: data = 10000000.0 class Solution: def minDiff(self, root, K): ans = Ans() solve(root, K, ans) return ans.data
FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys def inorder(root, arr): if root is None: return inorder(root.left, arr) arr.append(root.data) inorder(root.right, arr) class Solution: def minDiff(self, root, K): arr = [] inorder(root, arr) min_abs = sys.maxsize for i in arr: min_abs = min(min_abs, abs(i - K)) return min_abs
IMPORT FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): return self._min(root, K) def _min(self, node, k, diff=None): if node is None: return diff if diff is None or abs(node.data - k) <= diff: diff = abs(node.data - k) if k == node.data: return 0 if k < node.data: return self._min(node.left, k, diff) if k > node.data: return self._min(node.right, k, diff)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF NONE IF VAR NONE RETURN VAR IF VAR NONE FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): if not root: return -1 f = self.floor(root, K) c = self.ceil(root, K) if f == -1: return c - K elif c == -1: return K - f else: return min(K - f, c - K) return f, c def floor(self, root, K): if not root: return -1 if root.data == K: return root.data elif root.data < K: res = self.floor(root.right, K) if res == -1: return root.data else: return max(res, root.data) else: return self.floor(root.left, K) def ceil(self, root, K): if not root: return -1 if root.data == K: return root.data elif root.data < K: return self.ceil(root.right, K) else: res = self.ceil(root.left, K) if res == -1: return root.data else: return min(res, root.data)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): if root is None: return ilst = [] def inorder(node): if not node: return [] inorder(node.left) ilst.append(node.data) inorder(node.right) inorder(root) def dist(x): return abs(x - K) ans = min([dist(i) for i in ilst]) return ans
CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR LIST FUNC_DEF IF VAR RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): curr = root curr_min = 2**63 while curr is not None: curr_min = min(abs(K - curr.data), curr_min) if curr_min == 0: return 0 if K < curr.data: curr = curr.left elif K > curr.data: curr = curr.right return curr_min
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: def util(self, root, k): global ans if root == None: return if root.data == k: ans = min(abs(root.data - k), ans) return elif root.data < k: ans = min(abs(root.data - k), ans) self.util(root.right, k) else: ans = min(abs(root.data - k), ans) self.util(root.left, k) def minDiff(self, root, K): global ans ans = sys.maxsize self.util(root, K) return ans
IMPORT CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): if not root: return val = root.data while root: if abs(K - root.data) <= abs(K - val): val = root.data if K > root.data: root = root.right elif K < root.data: root = root.left else: return 0 return abs(K - val)
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR VAR WHILE VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def __init__(self): self.l = [] def minDiff(self, root, K): self.search(root, K, list) if K >= self.l[-1]: return abs(K - self.l[-1]) if K <= self.l[0]: return abs(self.l[0] - K) for i in range(len(self.l)): if self.l[i] == K: return 0 if self.l[i] < K and self.l[i + 1] > K: return min(abs(self.l[i] - K), abs(self.l[i + 1] - K)) def search(self, root, k, list): if root is None: return self.search(root.left, k, list) self.l.append(root.data) self.search(root.right, k, list)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): minimum = [float("inf")] self.helper(root, K, minimum) return minimum[0] def helper(self, root, K, minimum): if root is None: return minimum[0] = min(minimum[0], abs(root.data - K)) if root.data == K: return elif root.data > K: self.helper(root.left, K, minimum) elif root.data < K: self.helper(root.right, K, minimum)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR NONE RETURN ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def fun(self, root, k, h): if root == None: return h.append(abs(root.data - k)) Solution.fun(self, root.left, k, h) Solution.fun(self, root.right, k, h) return h def minDiff(self, root, K): h = [] a = Solution.fun(self, root, K, h) return min(a)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): res = [] def helper(node): if not node: return helper(node.left) res.append(node.data) helper(node.right) helper(root) mindiff = float("inf") for i in res: min_val = abs(K - i) mindiff = min(mindiff, min_val) return mindiff
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = [float("inf")] def helper(root, k, ans): if not root: return 0 ans[0] = min(ans[0], abs(k - root.data)) return helper(root.left, k, ans) or helper(root.right, k, ans) val = helper(root, k, ans) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def Preorder(self, root, k, ans): if root == None: return if abs(root.data - k) < ans[0]: ans[0] = abs(root.data - k) self.Preorder(root.left, k, ans) self.Preorder(root.right, k, ans) def minDiff(self, root, K): ans = [10**9] self.Preorder(root, K, ans) return ans[0]
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, k): ans = [9999999999999999999] def solve(root): if not root: return ans[0] = min(abs(root.data - k), ans[0]) solve(root.left) solve(root.right) solve(root) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR RETURN ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): m = float("inf") def solve(root, k): nonlocal m if root: if root.data == k: m = 0 return elif root.data > k: m = min(m, root.data - k) solve(root.left, k) else: m = min(m, k - root.data) solve(root.right, k) solve(root, k) return m
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR NUMBER RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): q = [] ans = 10000000 q.append(root) while q: node = q.pop(0) ans = min(ans, abs(K - node.data)) if node.left: q.append(node.left) if node.right: q.append(node.right) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: def minDiff(self, root, K): def inorder(root): nonlocal res if not root: return inorder(root.left) res = min(abs(root.data - K), res) inorder(root.right) res = sys.maxsize inorder(root) return res
IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): largest, smallest = -(2**63), 2**63 def for_large(root, k): ans = 2**63 while root: if root.data == k: return k if root.data > k: ans = min(ans, root.data) root = root.left else: root = root.right return ans def for_small(root, k): ans = -(2**63) while root: if root.data == k: return k if root.data < k: ans = max(ans, root.data) root = root.right else: root = root.left return ans l, s = for_large(root, K), for_small(root, K) ans = min(abs(l - K), abs(K - s)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: val = float("inf") def minDiff(self, root, K): if root == None: return self.val = min(self.val, abs(root.data - K)) self.minDiff(root.left, K) self.minDiff(root.right, K) return self.val
CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): diff = 2**31 - 1 ans = self.calculate_diff(root, k, diff) return ans def calculate_diff(self, root, k, diff): if not root: return 2**31 - 1 diff = min(diff, abs(root.data - k)) lt = self.calculate_diff(root.left, k, diff) rt = self.calculate_diff(root.right, k, diff) return min(diff, min(lt, rt))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def __init__(self): self.res = float("inf") def check(self, a, k): if a == None: return self.res = min(self.res, abs(a.data - k)) if a.data <= k: self.check(a.right, k) else: self.check(a.left, k) def minDiff(self, root, K): self.check(root, K) return self.res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def __init__(self): self.arr = [] def inOrder(self, head): if head == None: return self.inOrder(head.left) self.arr.append(head.data) self.inOrder(head.right) def minDiff(self, root, K): self.inOrder(root) if K < self.arr[0]: return abs(K - self.arr[0]) n = len(self.arr) for i in range(n - 1): a = self.arr[i] b = self.arr[i + 1] if K == a: return 0 elif a < K and K < b: return min(abs(a - K), abs(b - K)) return abs(K - self.arr[n - 1])
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 FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, k): self.ans = 10**9 def func(root, k): if root is None: return func(root.left, k) self.ans = min(self.ans, abs(root.data - k)) func(root.right, k) func(root, k) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): v = [] self.inorder(root, v) v.sort() minn = 999999999 for i in range(len(v)): if minn > abs(K - v[i]): minn = abs(K - v[i]) return minn def inorder(self, root, v): if root == None: return self.inorder(root.left, v) v.append(root.data) self.inorder(root.right, v)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR 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 BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def Preorder(self, root, K): temp = root ans = 2**63 while temp != None: ans = min(ans, abs(K - temp.data)) if temp.data == K: return 0 elif temp.data < K: temp = temp.right else: temp = temp.left return ans def minDiff(self, root, K): val = self.Preorder(root, K) return val
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def util(self, root, ans, k): if not root: return None if root.data == k: ans[0] = 0 return None ans[0] = min(abs(root.data - k), ans[0]) if root.data < k: self.util(root.right, ans, k) else: self.util(root.left, ans, k) def minDiff(self, root, K): ans = [float("inf")] self.util(root, ans, K) return ans[0]
CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN NONE ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: diff = sys.maxsize def minDiff(self, root, K): if root == None: return 0 self.diff = min(self.diff, abs(K - root.data)) if root.data > K: self.minDiff(root.left, K) if root.data < K: self.minDiff(root.right, K) return self.diff
IMPORT CLASS_DEF ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: maxVal = -1 res = 10**5 def minDiff(self, root, K): if root: if abs(root.data - K) < abs(self.maxVal - K): self.maxVal = root.data self.res = abs(root.data - K) if K < root.data: self.minDiff(root.left, K) return self.res else: self.minDiff(root.right, K) return self.res return self.res
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: mini = float("inf") def minDiff(self, root, k): if root: if root.data > k: temp = root.data - k self.mini = min(temp, self.mini) self.minDiff(root.left, k) elif root.data < k: temp = k - root.data self.mini = min(temp, self.mini) self.minDiff(root.right, k) else: self.mini = 0 return self.mini
CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: ans = -1 def findmin(self, node, K): if node is None: return if abs(K - node.data) < self.ans or self.ans == -1: self.ans = abs(K - node.data) if node.data < K: self.findmin(node.right, K) else: self.findmin(node.left, K) def minDiff(self, root, K): self.findmin(root, K) return self.ans
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def inorder(self, root): res = [] q = [] cur = root while True: if cur is not None: q.append(cur) cur = cur.left elif q: cur = q.pop() res.append(cur.data) cur = cur.right else: break return res def minDiff(self, root, K): ino = self.inorder(root) p_index = -1 n = len(ino) for i in range(n): if ino[i] > K: p_index = i break elif ino[i] == K: return 0 if p_index == -1: return K - ino[n - 1] if p_index > 0 and p_index < n: if ino[p_index] - K > K - ino[p_index - 1]: return K - ino[p_index - 1] else: return ino[p_index] - K if p_index == 0: return ino[p_index] - K
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): b = [] tmp = root Solution.bst(tmp, b) b.sort() x = K - b[0] for i in range(1, len(b)): if abs(K - b[i]) < x: x = abs(K - b[i]) if K - b[i] == 0: return 0 return x def bst(root, b): b.append(root.data) if root.left != None: Solution.bst(root.left, b) if root.right != None: Solution.bst(root.right, b) return
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR RETURN
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): smaller = -float("inf") bigger = float("inf") def recursive(root, k): nonlocal smaller, bigger if root is None: return if root.data < k and smaller < root.data: smaller = root.data elif root.data >= k and root.data < bigger: bigger = root.data recursive(root.left, k) recursive(root.right, k) recursive(root, K) return min(K - smaller, bigger - K) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def __init__(self): self.list1 = [] def minDiff(self, root, K): def traversal(root): nonlocal value, minimum if root == None: return None if abs(root.data - K) < minimum: minimum = abs(root.data - K) value = root.data if root.left: traversal(root.left) if root.right: traversal(root.right) value = None minimum = 999999 traversal(root) return minimum
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
def minDiffHelper(root, k, lastDiff): if root: df = abs(k - root.data) if df == 0: lastDiff[0] = df return if df < lastDiff[0]: lastDiff[0] = df if k < root.data: minDiffHelper(root.left, k, lastDiff) if k > root.data: minDiffHelper(root.right, k, lastDiff) class Solution: def minDiff(self, root, K): l = [10000000.0] minDiffHelper(root, K, l) return l[0]
FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR RETURN IF VAR VAR NUMBER ASSIGN VAR NUMBER 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 LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = [10000000] * 1 def fun(root): if root == None: return if abs(root.data - K) < ans[0]: ans[0] = abs(root.data - K) fun(root.left) fun(root.right) fun(root) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, k): maxi = [] mini = [] self.res = None def func(root, k): if root is None: return func(root.left, k) if root.data == k: self.res = 0 elif root.data > k: maxi.append(root.data) elif root.data < k: mini.append(root.data) func(root.right, k) func(root, k) if self.res == 0: return 0 elif maxi and mini: ans = min(abs(maxi[0] - k), abs(mini[-1] - k)) return ans elif maxi: return abs(maxi[0] - k) else: return abs(mini[-1] - k)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR IF VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, k): diff = float("inf") while root: diff = min(diff, abs(root.data - k)) if k < root.data: root = root.left elif k > root.data: root = root.right else: return 0 return diff
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
import sys class Solution: def minDiff(self, root, K): l, g = [-sys.maxsize - 1], [sys.maxsize + 1] def f(t): if t == None: return None if t.data == K: return 0 elif t.data > K: g[0] = t.data return f(t.left) else: l[0] = t.data return f(t.right) if f(root) == 0: return 0 return min(abs(K - l[0]), abs(g[0] - K))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): def check(root, k): nonlocal mn if root == None: return mn = min(mn, abs(root.data - k)) check(root.left, k) check(root.right, k) mn = 10**9 + 7 check(root, K) return mn
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: diff = float("inf") def minDiff(self, r, k): self.d(r, k) return self.diff return 1 def d(self, r, k): if r: if abs(k - r.data) < abs(self.diff): self.diff = abs(k - r.data) self.d(r.left, k) self.d(r.right, k)
CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
def minDiff(self, root, k): if root is None: return 999999999 if root.data == k: return 0 if root.data > k: return min(abs(root.data - k), self.minDiff(root.left, k)) return min(abs(root.data - k), self.minDiff(root.right, k)) class Solution: def solve(self, root, K, mn): if root is None: return mn.append(abs(root.data - K)) self.solve(root.left, K, mn) self.solve(root.right, K, mn) def minDiff(self, root, K): if root is None: return 999999 if root.data == K: return 0 if root.data > K: return min(abs(root.data - K), self.minDiff(root.left, K)) return min(abs(root.data - K), self.minDiff(root.right, K))
FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = 10**7 def helper(root, K): nonlocal ans if root: ans = min(ans, abs(K - root.data)) if K > root.data: helper(root.right, K) else: helper(root.left, K) helper(root, K) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = abs(root.data - k) while root != None: ans = min(ans, abs(root.data - k)) if root.data < k: root = root.right else: root = root.left return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def getNextMaxNode(self, currentNode, previousMax): if currentNode is None: return None if currentNode.data >= previousMax: leftSubTreeNextMax = self.getNextMaxNode(currentNode.left, previousMax) if leftSubTreeNextMax is not None: return leftSubTreeNextMax return currentNode return self.getNextMaxNode(currentNode.right, previousMax) def getNextMinNode(self, currentNode, previousMin): if currentNode is None: return None if currentNode.data <= previousMin: rightSubTreeNextMax = self.getNextMinNode(currentNode.right, previousMin) if rightSubTreeNextMax is not None: return rightSubTreeNextMax return currentNode return self.getNextMinNode(currentNode.left, previousMin) def minDiff(self, root, K): maxDiff = 1000000000 nextMaxNode = self.getNextMaxNode(root, K) nextMinNode = self.getNextMinNode(root, K) if nextMaxNode is not None: maxDiff = abs(nextMaxNode.data - K) if nextMinNode is not None: maxDiff = min(abs(nextMinNode.data - K), maxDiff) return maxDiff
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR
Given a BST and an integer. Find the least absolute difference between any node value of the BST and the given integer. Example 1: Input: 10 / \ 2 11 / \ 1 5 / \ 3 6 \ 4 K = 13 Output: 2 Explanation: K=13. The node that has value nearest to K is 11. so the answer is 2 Example 2: Input: 8 / \ 1 9 \ \ 4 10 / 3 K = 9 Output: 0 Explanation: K=9. The node that has value nearest to K is 9. so the answer is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function minDiff() that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5 }1 <= Value stored at nodes(data), K <= 10^{5}
class Solution: def minDiff(self, root, K): ans = float("inf") while root: if root.data == K: return 0 elif root.data > K: if ans > root.data - K: ans = root.data - K root = root.left else: if ans > K - root.data: ans = K - root.data root = root.right return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def constructBST(self, leftHead, rightHead): if leftHead == rightHead: return None slow, fast = leftHead, leftHead while fast != rightHead and fast.next != rightHead: slow = slow.next fast = fast.next.next root = TNode(slow.data) root.left = self.constructBST(leftHead, slow) root.right = self.constructBST(slow.next, rightHead) return root def sortedListToBST(self, head): if not head: return None if not head.next: root = TNode(head.data) return root return self.constructBST(head, None)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NONE
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if not head: return if not head.next: l = TNode(head.data) return l p, q = head, head while q and q.next: r = p p = p.next q = q.next if q: q = q.next right = p right = right.next r.next = None t = TNode(p.data) t.left = self.sortedListToBST(head) t.right = self.sortedListToBST(right) return t
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): arr = [] ptr = head while ptr != None: arr.append(ptr.data) ptr = ptr.next return self.construct_BST(None, arr) def construct_BST(self, root, arr): n = len(arr) if n == 0: return None index = int(n / 2) root = TNode(arr[index]) root.left = self.construct_BST(root.left, arr[:index]) root.right = self.construct_BST(root.right, arr[index + 1 :]) return root
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR NONE VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def getmid(self, root): if not root: return None prev = slow = fast = root while fast and fast.next: prev = slow slow = slow.next fast = fast.next.next if prev: prev.next = None return slow def sortedListToBST(self, head): if not head: return None mid = self.getmid(head) root = TNode(mid.data) if mid == head: return root root.left = self.sortedListToBST(head) root.right = self.sortedListToBST(mid.next) return root
CLASS_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): data = [] temp = head while temp: data.append(temp.data) temp = temp.next def midvalueBST(low, high, data): mid = (low + high) // 2 if (low + high) % 2 == 0: return mid else: return mid + 1 def constBST(low, high, data): if low > high: return None mid = midvalueBST(low, high, data) root = TNode(data[mid]) root.left = constBST(low, mid - 1, data) root.right = constBST(mid + 1, high, data) return root return constBST(0, len(data) - 1, data)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def __init__(self): self.h = None def sortedListToBST(self, head): n = 0 p = head while p: n += 1 p = p.next def f(l, h): if l > h: return None if (l + h) % 2 == 0: mid = (l + h) // 2 else: mid = (l + h) // 2 + 1 left = f(l, mid - 1) node = TNode(self.h.data) self.h = self.h.next node.left = left node.right = f(mid + 1, h) return node self.h = head return f(0, n - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NONE IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): self.List = [] node = head while node is not None: self.List.append(node.data) node = node.next treeHead = self.populateTree(0, len(self.List)) return treeHead def populateTree(self, st, ed): if st == ed: return None mid = (st + ed) // 2 node = TNode(self.List[mid]) if ed - st == 1: return node node.left = self.populateTree(st, mid) node.right = self.populateTree(mid + 1, ed) return node
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): def fun(low, high, a): if (low + high) % 2 == 0: mid = (low + high) // 2 return mid else: mid = (low + high) // 2 return mid + 1 a = [] temp = head while temp: a.append(temp.data) temp = temp.next def make(low, high, a): if low > high: return None mid = fun(low, high, a) root = TNode(a[mid]) root.left = make(low, mid - 1, a) root.right = make(mid + 1, high, a) return root return make(0, len(a) - 1, a)
CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Node: def __init__(self, data): self.data = data self.left = self.right = None def solve(head): if head is None: return if head.next is None: return Node(head.data) curr = head fast = head slow = head while fast and fast.next: curr = slow fast = fast.next.next slow = slow.next root = Node(slow.data) temp = slow.next curr.next = None root.left = solve(head) root.right = solve(temp) return root class Solution: def sortedListToBST(self, head): return solve(head)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF IF VAR NONE RETURN IF VAR NONE RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): def center(node): slow = node fast = node.next.next while fast and fast.next: slow = slow.next fast = fast.next.next return slow def f(node): if not node: return None if not node.next: return TNode(node.data) if not node.next.next: left = TNode(node.data) t = TNode(node.next.data) t.left = left return t cnode = center(node) rnode = cnode.next cnode.next = None left = f(node) right = f(rnode.next) t = TNode(rnode.data) t.left = left t.right = right return t return f(head)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NONE IF VAR RETURN FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def bst(self, li): if not li: return None cen = len(li) // 2 head = TNode(li[cen]) head.left = self.bst(li[:cen]) head.right = self.bst(li[cen + 1 :]) return head def sortedListToBST(self, head): data = [] while head != None: data.append(head.data) head = head.next return self.bst(data)
CLASS_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): List = [] cur = head while cur: List.append(cur.data) cur = cur.next mid = len(List) // 2 def CreateTree(l, r): if l >= r: return mid = (l + r) // 2 Tree = TNode(List[mid]) Tree.left = CreateTree(l, mid) Tree.right = CreateTree(mid + 1, r) return Tree return CreateTree(0, len(List))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def __init__(self): self.head = None def Length(self, head): count = 0 temp = head while temp != None: count += 1 temp = temp.next return count def SortedListToBST(self, n): if n <= 0: return None l = self.SortedListToBST(n // 2) root = TNode(self.head.data) self.head = self.head.next root.left = l root.right = self.SortedListToBST(n - n // 2 - 1) return root def sortedListToBST(self, head): self.head = head n = self.Length(head) root = self.SortedListToBST(n) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
import sys sys.setrecursionlimit(10**7) class Solution: def count_nodes(self, head): if head is None: return 0 count = 1 curr = head while curr.next: curr = curr.next count += 1 return count def sortedListToBST_util(self, head, n): if n == 0: return None left = self.sortedListToBST_util(head, n // 2) root = TNode(head[0].data) head[0] = head[0].next root.left = left root.right = self.sortedListToBST_util(head, n - n // 2 - 1) return root def sortedListToBST(self, head): n = self.count_nodes(head) return self.sortedListToBST_util([head], n)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): t = head a = [] while t: a.append(t.data) t = t.next def build(a, s, e): if s > e: return None if (s + e) % 2 == 0: m = (s + e) // 2 else: m = (s + e) // 2 + 1 node = TNode(a[m]) node.left = build(a, s, m - 1) node.right = build(a, m + 1, e) return node return build(a, 0, len(a) - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NONE IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): elems = [] while head: elems.append(head.data) head = head.next return self._sortedListToBST(elems, 0, len(elems) - 1) def _sortedListToBST(self, elems, start, end): if end < start: return None m = (start + end + 1) // 2 node = TNode(elems[m]) node.left = self._sortedListToBST(elems, start, m - 1) node.right = self._sortedListToBST(elems, m + 1, end) return node
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def divide(self, head): l = 0 temp = head while temp: l += 1 temp = temp.next Llen = l // 2 - 1 temp = head for i in range(Llen): temp = temp.next rest = temp.next temp.next = None mid = rest rest = rest.next mid.next = None return head, mid, rest def util(self, root, head): if head == None: return None if head.next == None: return TNode(head.data) l, m, r = self.divide(head) root = TNode(m.data) root.left = self.util(root, l) root.right = self.util(root, r) return root def sortedListToBST(self, head): root = self.util(None, head) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR NONE RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if not head: return None _res = [] while head: _res.append(head.data) head = head.next return self._bst(_res, 0, len(_res) - 1) def _bst(self, _res, low, high): if low > high: return None if (low + high) % 2 == 0: mid = (low + high) // 2 else: mid = (low + high) // 2 mid += 1 root = TNode(_res[mid]) root.left = self._bst(_res, low, mid - 1) root.right = self._bst(_res, mid + 1, high) return root
CLASS_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): nodes = 0 curr = head while curr: nodes += 1 curr = curr.next def make_tree(curr, subset): if subset == 1: return TNode(curr.data), curr half = subset // 2 left_root, curr = make_tree(curr, half) curr = curr.next root = TNode(curr.data) root.left = left_root other_half = subset - (half + 1) if other_half != 0: root.right, curr = make_tree(curr.next, other_half) return root, curr curr = head return make_tree(curr, nodes)[0]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): a = [] while head is not None: a.append(TNode(head.data)) head = head.next return self.sortedArrayToBST(a) def sortedArrayToBST(self, nums): if len(nums) == 0: return mid = len(nums) // 2 ans = nums[mid] ans.left = self.sortedArrayToBST(nums[0:mid]) ans.right = self.sortedArrayToBST(nums[mid + 1 :]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def getMidNode(self, head): prev = None mid = head q = head.next while q is not None: prev = mid mid = mid.next q = q.next if q is not None: q = q.next return prev, mid def sortedListToBST(self, head): if head is None: return None prev, mid = self.getMidNode(head) if prev is None: ll = None else: ll = head prev.next = None rl = mid.next mid.next = None root = TNode(mid.data) root.left = self.sortedListToBST(ll) root.right = self.sortedListToBST(rl) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): def create(head): if head is None: return head if head.next is None: return TNode(head.data) pre = None slow = head forw = head while forw is not None and forw.next is not None: pre = slow slow = slow.next forw = forw.next.next pre.next = None newNode = TNode(slow.data) newNode.left = create(head) newNode.right = create(slow.next) return newNode return create(head)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): def _convert_linked_to_list(head): to_return = [] this_node = head while this_node.next: to_return.append(this_node.data) this_node = this_node.next to_return.append(this_node.data) return to_return def _build_BTS(list_segment): if not list_segment: return None center_i = int(len(list_segment) / 2) center_node = TNode(list_segment[center_i]) center_node.left = _build_BTS(list_segment[:center_i]) center_node.right = _build_BTS(list_segment[center_i + 1 :]) return center_node as_list = _convert_linked_to_list(head) return _build_BTS(as_list)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if not head: return None return self.bst(head, None) def bst(self, head, tail): if head == tail: return None fast = slow = head while fast != tail and fast.next != tail: fast = fast.next.next slow = slow.next node = TNode(slow.data) node.left = self.bst(head, slow) node.right = self.bst(slow.next, tail) return node
CLASS_DEF FUNC_DEF IF VAR RETURN NONE RETURN FUNC_CALL VAR VAR NONE FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if head is None: return None prev, slow, fast = None, head, head while fast and fast.next: prev = slow slow = slow.next fast = fast.next.next if prev is not None: prev.next = None node = TNode(slow.data) node.left = self.sortedListToBST(head if prev else None) node.right = self.sortedListToBST(slow.next) return node
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR NONE VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): temp = head l1 = [] while temp: l1.append(temp.data) temp = temp.next def construct(l, h): if l > h: return None mid = (l + h + 1) // 2 nn = TNode(l1[mid]) nn.left = construct(l, mid - 1) nn.right = construct(mid + 1, h) return nn return construct(0, len(l1) - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): val_list = [] while head: val_list.append(head.data) head = head.next def helper(arr): if not arr: return None else: m = len(arr) // 2 node = TNode(arr[m]) node.left = helper(arr[:m]) node.right = helper(arr[m + 1 :]) return node root = helper(val_list) return root
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): arr = [] length = 0 temp = head while temp: arr.append(temp.data) length += 1 temp = temp.next def midcal(low, high): if (low + high) % 2 == 0: return (low + high) // 2 return (low + high) // 2 + 1 def Tree(low, high, data): if low > high: return None mid = midcal(low, high) root = TNode(arr[mid]) root.left = Tree(low, mid - 1, arr) root.right = Tree(mid + 1, high, arr) return root return Tree(0, length - 1, arr)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): self.lst = [] while head: self.lst.append(head.data) head = head.next def dfs(lst): if lst == []: return if len(lst) == 1: return TNode(lst[0]) mid = len(lst) // 2 node = TNode(lst[mid]) node.left = dfs(lst[:mid]) node.right = dfs(lst[mid + 1 :]) return node return dfs(self.lst)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR LIST RETURN IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def solve(self, head, n): if n <= 0 or head[0] == None: return mid = n // 2 left = self.solve(head, mid) root = TNode(head[0].data) root.left = left head[0] = head[0].next root.right = self.solve(head, n - mid - 1) return root def sortedListToBST(self, head): count = 0 temp = head while temp: count += 1 temp = temp.next head = [head] root = self.solve(head, count) return root
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER NONE RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if not head: return None if not head.next: return TNode(head.data) prev = LNode(-1) prev.next = slow = head fast = head while slow and fast and fast.next: slow = slow.next fast = fast.next.next prev = prev.next root = TNode(slow.data) prev.next = None root.left = self.sortedListToBST(head) root.right = self.sortedListToBST(slow.next) return root
CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): n = countNodes(head) return sortedListToBSTRecur([head], n) def sortedListToBSTRecur(head_ref, n): if n <= 0: return None left = sortedListToBSTRecur(head_ref, n // 2) root = TNode(head_ref[0].data) root.left = left head_ref[0] = head_ref[0].next root.right = sortedListToBSTRecur(head_ref, n - n // 2 - 1) return root def countNodes(head): count = 0 current = head while current: count += 1 current = current.next return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): cnt = 0 temp = head while temp: temp = temp.next cnt += 1 self.head = head return self.func(cnt) def func(self, n): if n < 1: return left = self.func(int(n / 2)) root = TNode(self.head.data) root.left = left self.head = self.head.next root.right = self.func(n - int(n / 2) - 1) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class LNode: def __init__(self, data): self.data = data self.next = None class TNode: def __init__(self, data): self.data = data self.left = self.right = None class Solution: def sortedListToBST(self, head): def build(a, mid, root): left = [] right = [] if root: root.data = a[mid] left = a[:mid] right = a[mid + 1 :] if left: root.left = TNode(None) build(left, len(left) // 2, root.left) if right: root.right = TNode(None) build(right, len(right) // 2, root.right) a = [] temp = head while temp: a.append(temp.data) temp = temp.next n = len(a) mid = n // 2 root = TNode(a[mid]) build(a, mid, root) return root
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): if not head: return None if not head.next: return TNode(head.data) slow, fast = head, head.next.next while fast and fast.next: slow = slow.next fast = fast.next.next root = TNode(slow.next.data) right_head = slow.next.next slow.next = None root.right = self.sortedListToBST(right_head) root.left = self.sortedListToBST(head) return root
CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def findMiddle(self, head): if head == None or head.next == None: return head tur = head hare = head.next while hare.next and hare.next.next: tur = tur.next hare = hare.next.next return tur def buildBST(self, head): if head == None: return head if head.next == None: node = TNode(head.data) return node middle = self.findMiddle(head) nxt = middle.next middle.next = None root = TNode(nxt.data) root.left = self.buildBST(head) root.right = self.buildBST(nxt.next) return root def sortedListToBST(self, head): if head == None: return None if head.next == None: return TNode(head.data) if head.next.next == None: node = TNode(head.next.data) node.left = TNode(head.data) return node return self.buildBST(head)
CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR NONE RETURN FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List. Note: There might be nodes with the same value. Example 1: Input: Linked List: 1->2->3->4->5->6->7 Output: 4 2 1 3 6 5 7 Explanation : The BST formed using elements of the linked list is, 4 / \ 2 6 / \ / \ 1 3 5 7 Hence, preorder traversal of this tree is 4 2 1 3 6 5 7 Example 2: Input: Linked List : 1->2->3->4 Ouput: 3 2 1 4 Explanation: The BST formed using elements of the linked list is, 3 / \ 2 4 / 1 Hence, the preorder traversal of this tree is 3 2 1 4 Your task : You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created. Expected Time Complexity: O(N), N = number of Nodes Expected Auxiliary Space: O(N), N = number of Nodes Constraints: 1 ≤ Number of Nodes ≤ 10^{6} 1 ≤ Value of each node ≤ 10^{6}
class Solution: def sortedListToBST(self, head): cur = head n = 0 while cur: n += 1 cur = cur.next return self.solver(head, n) def solver(self, head, n): if n == 0 or head == None: return None mid = n // 2 i = 0 temp = head while i != mid: temp = temp.next i += 1 head1 = TNode(temp.data) head1.left = self.solver(head, n // 2) if n % 2 == 1: head1.right = self.solver(temp.next, n // 2) else: head1.right = self.solver(temp.next, n // 2 - 1) return head1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NONE RETURN NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR