description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(Number of nodes) Expected Auxiliary Space: O(logN), N = number of nodes Constraints: 1 <= Number of nodes <= 10^5
class Solution: def correctBST(self, root): inorder = [] def inOrder(root): if root is None: return inOrder(root.left) inorder.append(root.data) inOrder(root.right) inOrder(root) inorder.sort() self.i = 0 def inOrder2(root): if root is None: return inOrder2(root.left) if inorder[self.i] != root.data: root.data = inorder[self.i] self.i += 1 inOrder2(root.right) inOrder2(root) return root
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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, n, Q, nums, query): prefixXor = [0] for i in range(n): prefixXor.append(prefixXor[-1] ^ nums[i]) ans = [] for l, r in query: ans.append(prefixXor[r] ^ prefixXor[l - 1] ^ prefixXor[-1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, Query): n_xor = 0 for i in a: n_xor ^= i seg = [0] * (4 * N) def query(idx, low, high, l, r): if low >= l and high <= r: return seg[idx] if high < l or low > r: return 0 mid = (low + high) // 2 left = query(2 * idx + 1, low, mid, l, r) right = query(2 * idx + 2, mid + 1, high, l, r) return left ^ right def build(idx, low, high): if low == high: seg[idx] = a[low] return mid = (low + high) // 2 build(2 * idx + 1, low, mid) build(2 * idx + 2, mid + 1, high) seg[idx] = seg[2 * idx + 1] ^ seg[2 * idx + 2] build(0, 0, N - 1) ans = [] for i, j in Query: x = query(0, 0, N - 1, i - 1, j - 1) ans.append(n_xor ^ x) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): x = [0] * (N + 1) for i in range(1, N + 1): x[i] = x[i - 1] ^ a[i - 1] final = [] for i in query: l, r = i[0], i[1] s = x[l - 1] t = x[-1] ^ x[r] final.append(s ^ t) return final
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class segmentTree: def __init__(self, arr): self.size = 1 while self.size < len(arr): self.size *= 2 self.values = [0] * (2 * self.size - 1) self.build(arr, 0, self.size, 0) def build(self, arr, left, right, i): if right - left == 1: if left < len(arr): self.values[i] = arr[left] return mid = (left + right) // 2 self.build(arr, left, mid, 2 * i + 1) self.build(arr, mid, right, 2 * i + 2) self.values[i] = self.values[2 * i + 1] ^ self.values[2 * i + 2] def getQuery(self, a, b, left, right, i): if a >= right or b <= left: return 0 if a <= left and b >= right: return self.values[i] mid = (left + right) // 2 return self.getQuery(a, b, left, mid, 2 * i + 1) ^ self.getQuery( a, b, mid, right, 2 * i + 2 ) class Solution: def specialXor(self, N, Q, a, query): st = segmentTree(a) ans = [] for q in query: first = st.getQuery(0, q[0] - 1, 0, st.size, 0) second = st.getQuery(q[1], st.size, 0, st.size, 0) ans.append(second ^ first) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
def xor(t, s, e, l, r, i): if l <= s and r >= e: return t[i] if l > e or r < s: return 0 m = (s + e) // 2 return xor(t, m + 1, e, l, r, 2 * i + 2) ^ xor(t, s, m, l, r, 2 * i + 1) def buildTree(t, a, s, e, i): if s == e: t[i] = a[s] return t[i] mid = (s + e) // 2 l = buildTree(t, a, s, mid, i * 2 + 1) r = buildTree(t, a, mid + 1, e, i * 2 + 2) t[i] = l ^ r return t[i] class Solution: def specialXor(self, N, Q, a, query): ans = a[0] for i in range(1, N): ans ^= a[i] res = [] tree = [0] * (4 * N) buildTree(tree, a, 0, N - 1, 0) for q in query: l = q[0] - 1 r = q[1] - 1 x = xor(tree, 0, N - 1, l, r, 0) x = x ^ ans res.append(x) return res
FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): tree = [0] * (2 * N) def get(l, r): l += N - 1 r += N - 1 res = 0 while l <= r: if l % 2 == 1: res ^= tree[l] l += 1 if r % 2 == 0: res ^= tree[r] r -= 1 l //= 2 r //= 2 return res def update(pos, val): pos += N - 1 tree[pos] = val pos //= 2 while pos >= 1: tree[pos] = tree[2 * pos] ^ tree[2 * pos + 1] pos //= 2 for i, x in enumerate(a): update(i + 1, x) res = [] for a, b in query: res.append(get(1, a - 1) ^ get(b + 1, N)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): for i in range(1, len(a)): a[i] ^= a[i - 1] main = a[len(a) - 1] ans = [] val = 0 val1 = 0 for i in range(Q): if query[i][0] == 1: val = a[query[i][1] - 1] ans.append(main ^ val) else: val = a[query[i][1] - 1] ^ a[query[i][0] - 2] ans.append(main ^ val) return ans
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): pre = [a[0]] pos = [a[-1]] for i in a[1:]: pre.append(pre[-1] ^ i) for i in a[-2::-1]: pos.append(pos[-1] ^ i) ret = [] for i, j in query: if i - 2 < 0: ret.append(pos[N - j - 1]) elif N - j - 1 < 0: ret.append(pre[i - 2]) else: ret.append(pre[i - 2] ^ pos[N - j - 1]) return ret
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): arr = [a[0]] for i in range(1, N): arr.append(arr[-1] ^ a[i]) ans = [] for q in query: l, r = q l, r = l - 1, r - 1 if l == 0: ans.append(arr[N - 1] ^ arr[r]) else: ans.append(arr[N - 1] ^ arr[l - 1] ^ arr[r]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class STree: def __init__(self, n): self.BIT = [0] * (2 * N) self.N = n def build(self, A): for i in range(self.N): idx = i + self.N self.BIT[idx] = A[i] for i in range(self.N - 1, 0, -1): self.BIT[i] = self.BIT[2 * i] ^ self.BIT[2 * i + 1] def update(self, idx, val): idx += self.N self.BIT[idx] = val while idx > 1: self.BIT[idx >> 1] = self.BIT[idx] ^ self.BIT[idx ^ 1] idx >>= 1 def query(self, l, r): l += self.N r += self.N ans = 0 while l <= r: if l & 1: ans ^= self.BIT[l] l += 1 if r & 1 == 0: ans ^= self.BIT[r] r -= 1 l >>= 1 r >>= 1 return ans class Solution: def specialXor(self, N, Q, A, query): tree = STree(N) tree.build(A) ans = [] for q in query: l, r = q[0] - 1, q[1] - 1 if l == 0 and r == N - 1: ans.append(0) elif l == 0: ans.append(tree.query(r + 1, N - 1)) elif r == N - 1: ans.append(tree.query(0, l - 1)) else: ans.append(tree.query(0, l - 1) ^ tree.query(r + 1, N - 1)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
def build(tree, arr, start, end, treeIndex): if start == end: tree[treeIndex] = arr[start] return mid = (start + end) // 2 build(tree, arr, start, mid, treeIndex * 2 + 1) build(tree, arr, mid + 1, end, treeIndex * 2 + 2) tree[treeIndex] = tree[treeIndex * 2 + 1] ^ tree[treeIndex * 2 + 2] def Query(tree, start, end, left, right, treeIndex): if start > right or end < left: return 0 elif start >= left and end <= right: return tree[treeIndex] mid = (start + end) // 2 ans1 = Query(tree, start, mid, left, right, treeIndex * 2 + 1) ans2 = Query(tree, mid + 1, end, left, right, treeIndex * 2 + 2) return ans1 ^ ans2 class Solution: def specialXor(self, N, Q, a, query): tree = [0] * (4 * N + 1) build(tree, a, 0, N - 1, 0) ans = [] for i in query: l = i[0] r = i[1] ans1 = Query(tree, 0, N - 1, l - 1, r - 1, 0) ans.append(tree[0] ^ ans1) return ans
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, n, q, arr, query): lis1 = [arr[0]] xor1 = arr[0] xor2 = arr[-1] lis2 = [arr[-1]] for i in range(1, n): xor1 = xor1 ^ arr[i] lis1.append(xor1) xor2 = xor2 ^ arr[-1 * (i + 1)] lis2.append(xor2) xors = [] for i in range(0, len(query)): if query[i][0] != 1 and query[i][1] != len(arr): xor = lis1[query[i][0] - 2] ^ lis2[-1 * (query[i][1] + 1)] xors.append(xor) if query[i][0] == 1: xors.append(lis2[-1 * (query[i][1] + 1)]) if query[i][1] == len(arr): xors.append(lis1[query[i][0] - 1]) return xors
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): for i in range(1, len(a)): a[i] ^= a[i - 1] return [ (a[-1] ^ (a[r - 1] ^ (a[l - 2] if l - 2 >= 0 else 0))) for l, r in query ]
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class segmentTree: def __init__(self, N): self.n = N self.tree = [0] * 2 * N def build(self, arr): for i in range(self.n): self.tree[self.n + i] = arr[i] for i in range(self.n - 1, 0, -1): self.tree[i] = self.tree[i << 1] ^ self.tree[i << 1 | 1] def updateTreeNode(self, p, value): self.tree[p + self.n] = value p = p + self.n i = p while i > 1: self.tree[i >> 1] = self.tree[i] ^ self.tree[i ^ 1] i >>= 1 def query(self, l, r): res = 0 l += self.n r += self.n while l < r: if l & 1: res ^= self.tree[l] l += 1 if r & 1: r -= 1 res ^= self.tree[r] l >>= 1 r >>= 1 return res class Solution: def specialXor(self, N, Q, a, query): tree = segmentTree(N) tree.build(a) results = [] for interval in query: results.append(tree.query(0, interval[0] - 1) ^ tree.query(interval[1], N)) return results
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, N, Q, a, query): results = [] base = 0 forward = [] backward = [] for x in a: base ^= x forward.append(base) reverse = 0 for x in a[::-1]: reverse ^= x backward.insert(0, reverse) for begin, end in query: xor = 0 if begin - 1: xor ^= forward[begin - 2] if end < N: xor ^= backward[end] results.append(xor) return results
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
def solve(tree, start, end, ss, se, si): if se < start or ss > end: return 0 if ss <= start and se >= end: return tree[si] mid = (start + end) // 2 if ss > mid: return solve(tree, mid + 1, end, ss, se, si * 2 + 2) if se <= mid: return solve(tree, start, mid, ss, se, si * 2 + 1) left = solve(tree, start, mid, ss, se, si * 2 + 1) right = solve(tree, mid + 1, end, ss, se, si * 2 + 2) return left ^ right def build(tree, start, end, a, si): if start == end: tree[si] = a[start] return mid = (start + end) // 2 build(tree, start, mid, a, si * 2 + 1) build(tree, mid + 1, end, a, si * 2 + 2) tree[si] = tree[si * 2 + 1] ^ tree[si * 2 + 2] class Solution: def specialXor(self, N, Q, a, query): tree = [0] * (4 * N) build(tree, 0, N - 1, a, 0) total = 0 ans = [] for i in range(N): total ^= a[i] for i in range(Q): xor = solve(tree, 0, N - 1, query[i][0] - 1, query[i][1] - 1, 0) ans.append(xor ^ total) return ans
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
def build(a, l, r, tree, si): if l == r: tree[si] = a[l] return a[l] mid = (l + r) // 2 left = build(a, l, mid, tree, si * 2 + 1) right = build(a, mid + 1, r, tree, si * 2 + 2) tree[si] = left ^ right return tree[si] def queries(tree, s, e, l, r, si): if l <= s and r >= e: return tree[si] if l > e or r < s: return 0 mid = (s + e) // 2 return queries(tree, s, mid, l, r, si * 2 + 1) ^ queries( tree, mid + 1, e, l, r, si * 2 + 2 ) class Solution: def specialXor(self, N, Q, a, query): tree = [0] * (4 * N) ans = [] build(a, 0, N - 1, tree, 0) res = 0 for i in range(N): res = res ^ a[i] for i in range(Q): x = queries(tree, 0, N - 1, query[i][0] - 1, query[i][1] - 1, 0) x = x ^ res ans.append(x) return ans
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def specialXor(self, n, q, a, query): l = [0] t = 0 for val in a: t ^= val l.append(t) ans = [] for val in query: ans.append(l[val[1]] ^ l[val[0] - 1] ^ l[n]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Segment: def __init__(self, arr): self.arr = arr self.tree = [None for i in range(4 * len(self.arr))] def constructHelper(self, ss, se, si): if ss == se: self.tree[si] = self.arr[ss] return self.tree[si] mid = (ss + se) // 2 self.tree[si] = self.constructHelper( ss, mid, 2 * si + 1 ) ^ self.constructHelper(mid + 1, se, 2 * si + 2) return self.tree[si] def constructTree(self): self.constructHelper(0, len(self.arr) - 1, 0) def getXorHelper(self, qs, qe, ss, se, si): if qe < ss or qs > se: return 0 if qs <= ss and qe >= se: return self.tree[si] mid = (ss + se) // 2 return self.getXorHelper(qs, qe, ss, mid, 2 * si + 1) ^ self.getXorHelper( qs, qe, mid + 1, se, 2 * si + 2 ) def getXor(self, qs, qe): return self.getXorHelper(qs, qe, 0, len(self.arr) - 1, 0) class Solution: def specialXor(self, N, Q, a, query): seg = Segment(a) seg.constructTree() now = 0 curr = seg.getXor(0, N - 1) res = [] for i in range(Q): p, q = query[i][0] - 1, query[i][1] - 1 res.append(curr ^ seg.getXor(p, q)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class Solution: def build(self, index, low, high, arr, seg_arr): if low == high: seg_arr[index] = arr[low] return mid = low + (high - low) // 2 left_index = 2 * index + 1 right_index = 2 * index + 2 self.build(left_index, low, mid, arr, seg_arr) self.build(right_index, mid + 1, high, arr, seg_arr) seg_arr[index] = seg_arr[left_index] ^ seg_arr[right_index] return def query_res(self, index, low, high, arr, seg_arr, L, R): if high < L or R < low: return 0 if L <= low and high <= R: return seg_arr[index] mid = low + (high - low) // 2 left_index = 2 * index + 1 right_index = 2 * index + 2 left_val = self.query_res(left_index, low, mid, arr, seg_arr, L, R) right_val = self.query_res(right_index, mid + 1, high, arr, seg_arr, L, R) return left_val ^ right_val def specialXor(self, N, Q, a, query): xor = 0 for x in a: xor = xor ^ x seg_arr = [0] * (4 * N) self.build(0, 0, N - 1, a, seg_arr) tmp_list = [] for i in range(Q): L, R = query[i][0] - 1, query[i][1] - 1 val = self.query_res(0, 0, N - 1, a, seg_arr, L, R) tmp_list.append(val ^ xor) return tmp_list
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Nitika recently read about XOR operation and she got obssessed with it. She has an array a containing N Positive integers.She wants to perform Q queries on the array.In a query She gives two integers L and R.(1 based indexing).Now, she asks what is the xor of all the elements of the array after not including the subarray ranging from L to R (both inclusive).Nitika guarantees that in each query, The resulting array is non empty. The queries are given in a 2D matrix query having L and R for each entry. Example 1: Input: N = 10, Q = 3 a = {4, 7, 8, 5, 9, 6, 1, 0, 20, 10} query = {{3, 8},{1, 6},{2, 3}} Output: 29 31 17 Explaination: For the first query: The resulting array is: (4 ,7 ,20, 10). Their Xor will be: 29. For the Second query: The resulting array is: (1, 0, 20, 10). Their Xor will be: 31. For the Third query: The resulting array is: (4, 5, 9, 6, 1,0 ,20, 10). Their Xor will be: 17. Your Task: you do not need to read input or print anything. Your task is to complete the function specialXor() which takes N, Q, a[] and query as input parameters and returns a list containing answer for each query. Expected Time Complexity: O(N+Q*logN) Expected Auxiliary Space: O(N+Q) Constraints: 1 ≤ N, Q ≤ 10^{5} 1 ≤ A[i] ≤ 10^{9} 1 ≤ L, R ≤ N
class SegmentTree: def __init__(self, arr): self.arr = arr self.n = len(arr) self.seg = [0] * self.n * 4 def build(ind, low, high): if low == high: self.seg[ind] = arr[low] return mid = low + high >> 1 build(ind * 2 + 1, low, mid) build(ind * 2 + 2, mid + 1, high) self.seg[ind] = self.seg[ind * 2 + 1] ^ self.seg[ind * 2 + 2] build(0, 0, self.n - 1) def query(self, ind, low, high, l, r): if low > r or l > high: return 0 if low >= l and high <= r: return self.seg[ind] mid = low + high >> 1 left = self.query(ind * 2 + 1, low, mid, l, r) right = self.query(ind * 2 + 2, mid + 1, high, l, r) return left ^ right class Solution: def specialXor(self, N, Q, a, query): s = SegmentTree(a) ans = 0 for i in a: ans ^= i res = [] for i, j in query: temp = s.query(0, 0, N - 1, i - 1, j - 1) res.append(ans ^ temp) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def mini(root): while root.left != None: root = root.left return root.data def deleteNode(root, X): if root == None: return None if root.data > X: root.left = deleteNode(root.left, X) if root.data < X: root.right = deleteNode(root.right, X) if root.data == X: if root.left == None and root.right == None: return None if root.left != None and root.right == None: return root.left if root.right != None and root.left == None: return root.right if root.left != None and root.right != None: root.data = mini(root.right) root.right = deleteNode(root.right, root.data) return root
FUNC_DEF WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, key): def findRightToConnect(root): if root.right == None: return root return findRightToConnect(root.right) def helper(root): if root.left == None and root.right == None: return None if root.left == None: return root.right if root.right == None: return root.left rightChild = root.right leftChild = findRightToConnect(root.left) leftChild.right = rightChild return root.left def deleteBST(root, key): if key == root.data: return helper(root) cur = root while root: if key > root.data: if root.right and root.right.data == key: root.right = helper(root.right) break else: root = root.right elif root.left and root.left.data == key: root.left = helper(root.left) else: root = root.left return cur return deleteBST(root, key)
FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(node, X): root = __delete(node, X) return root def __delete(node, X): if not node: return elif node.data > X: node.left = __delete(node.left, X) elif node.data < X: node.right = __delete(node.right, X) elif not node.left: return node.right elif not node.right: return node.left else: succ = minNode(node.right) node.data = succ node.right = __delete(node.right, succ) return node def minNode(node): while node and node.left: node = node.left return node.data
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def getx(curr, c): while curr.left != None: curr = curr.left return curr.data def deleteNode(root, X): if root == None: return elif root.data > X: root.left = deleteNode(root.left, X) elif root.data < X: root.right = deleteNode(root.right, X) elif root.left == None: return root.right elif root.right == None: return root.left else: succ = getx(root.right, X) root.data = succ root.right = deleteNode(root.right, succ) return root
FUNC_DEF WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): if root is None: return root if X > root.data: root.right = deleteNode(root.right, X) elif X < root.data: root.left = deleteNode(root.left, X) else: if root.left is None: temp = root.right return temp if root.right is None: temp = root.left return temp else: succ = findInorderSuccessor(root) root.data = succ.data root.right = deleteNode(root.right, succ.data) return root def findInorderSuccessor(curr): curr = curr.right while curr and curr.left: curr = curr.left return curr
FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def min_value(root): cur = root while cur.left != None: cur = cur.left return cur def deleteNode(root, X): if root == None: return root if root.data > X: root.left = deleteNode(root.left, X) elif root.data < X: root.right = deleteNode(root.right, X) else: if root.left == None: temp = root.right root = None return temp if root.right == None: temp = root.left root = None return temp temp = min_value(root.right) root.data = temp.data root.right = deleteNode(root.right, temp.data) return root
FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def found_node(root, X, pre): if root == None: return -1 if root.data > X: pre[0] = root return found_node(root.left, X, pre) elif root.data < X: pre[0] = root return found_node(root.right, X, pre) else: return root def getInorderPre(curr): prev = None curr = curr.left while curr.right: prev = curr curr = curr.right return curr, prev def delete_node(curr, X, pre): if curr.left == None and curr.right == None: if pre[0].left == curr: pre[0].left = None else: pre[0].right = None elif curr.left == None: if pre[0].left == curr: pre[0].left = curr.right else: pre[0].right = curr.right elif curr.right == None: if pre[0].right == curr: pre[0].right = curr.left else: pre[0].left = curr.left else: greatestLT, prev = getInorderPre(curr) curr.data = greatestLT.data if prev == None: curr.left = greatestLT.left elif prev.left == greatestLT: prev.left = greatestLT.left else: prev.right = greatestLT.left return def deleteNode(root, X): if root == None: return root elif root.data == X: if root.left == None and root.right == None: return None elif root.left == None: deleted = root root = root.right deleted.right = None return root elif root.right == None: deleted = root root = root.left deleted.left = None return root else: greatestLT, prev = getInorderPre(root) root.data = greatestLT.data if prev == None: root.left = greatestLT.left elif prev.left == greatestLT: prev.left = greatestLT.left else: prev.right = greatestLT.left return root else: pre = [None] curr = found_node(root, X, pre) if curr != -1: delete_node(curr, X, pre) return root
FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NONE VAR NONE IF VAR NUMBER VAR ASSIGN VAR NUMBER NONE ASSIGN VAR NUMBER NONE IF VAR NONE IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NONE IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR LIST NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): if root == None: return root if root.data < X: root.right = deleteNode(root.right, X) elif root.data > X: root.left = deleteNode(root.left, X) elif root.left == None and root.right == None: return None elif root.left != None and root.right == None: return root.left elif root.left == None and root.right != None: return root.right else: root.data = min(root.right) root.right = deleteNode(root.right, root.data) return root def min(root): curr = root while curr.left != None: curr = curr.left return curr.data
FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): if root == None: return None if root.data < X: root.right = deleteNode(root.right, X) elif root.data > X: root.left = deleteNode(root.left, X) else: if root.left == None: return root.right if root.right == None: return root.left if root.right and root.left: temp = root.right while temp.left: temp = temp.left root.data = temp.data root.right = deleteNode(root.right, temp.data) return root
FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def find_l(root, prev): cur = root if cur.right == None: c = cur.data prev.left = cur.left return c while cur.right != None: prev = cur cur = cur.right c = cur.data prev.right = cur.left return c def deleteNode(root, X): prev = None if root.data == X: if root.left == None and root.right == None: return prev def d_n(root, X): if root: cur = root if root.data == X: if root.left: r = find_l(root.left, root) root.data = r return root if root.right: k = root.right if prev == None and k.left == None and k.right == None: return k prev.left = cur.right return root if root.left == None and root.right == None: if prev.left.data == X: prev.left = None return root if prev.right.data == X: prev.right = None return root prev = cur d_n(root.left, X) d_n(root.right, X) return root d = d_n(root, X) return d
FUNC_DEF ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE IF VAR VAR IF VAR NONE VAR NONE RETURN VAR FUNC_DEF IF VAR ASSIGN VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR IF VAR NONE VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def nextlargest(root): if root.left is None: return root.data return nextlargest(root.left) def delnode(root): if root.left is None: return root.right if root.right is None: return root.left root.data = nextlargest(root.right) root.right = deleteNode(root.right, root.data) return root def deleteNode(root, X): if root is None: return root if root.data == X: return delnode(root) if root.data < X: root.right = deleteNode(root.right, X) else: root.left = deleteNode(root.left, X) return root
FUNC_DEF IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, x): if root == None: return None if root.data == x: if root.left == None and root.right == None: return None if root.left == None and root.right is not None: return root.right if root.left is not None and root.right == None: return root.left else: root.data = smallest(root.right) root.right = deleteNode(root.right, root.data) elif root.data > x: root.left = deleteNode(root.left, x) else: root.right = deleteNode(root.right, x) return root def smallest(root): if root == None: return None while root.left: root = root.left return root.data
FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE WHILE VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): def minimum(root): if root.left is None: return root.data return minimum(root.left) def deleteHelper(root, data): if not root: return False, None if root.data < data: deleted, newRightNode = deleteHelper(root.right, data) root.right = newRightNode return deleted, root if root.data > data: deleted, newLeftNode = deleteHelper(root.left, data) root.left = newLeftNode return deleted, root if not root.left and not root.right: return True, None if root.left == None: return True, root.right if root.right == None: return True, root.left replacement = minimum(root.right) root.data = replacement deleted, newRightNode = deleteHelper(root.right, replacement) root.right = newRightNode return True, root def deleteData(root, x): deleted, newRoot = deleteHelper(root, x) if not deleted: return root return newRoot return deleteData(root, x)
FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN NUMBER NONE IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER NONE IF VAR NONE RETURN NUMBER VAR IF VAR NONE RETURN NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): node = root found = False parent = None side = 0 while node: parent = node if node.data == X: found = True break elif node.data < X: node = node.left side = -1 elif node.data > X: node = node.right side = 1 if not found: return root if node.left and node.right: successor = node.right parent = node while successor.left: parent = successor successor = successor.left if node is parent: parent.right = successor.right else: parent.left = successor.right node.data = successor.data elif node.left: if side == 1: parent.right = node.left elif side == -1: parent.left = node.left elif side == 0: return node.left elif node.right: if side == 1: parent.right = node.right elif side == -1: parent.left = node.right elif side == 0: return node.right elif side == 1: parent.right = None elif side == -1: parent.left = None elif side == 0: return None return root
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR IF VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER RETURN NONE RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def minValue(root): if root is None: return None while root.left is not None: root = root.left return root.data def deleteNode(root, X): if root is None: return None if X == root.data: if root.left is None: return root.right if root.right is None: return root.left else: root.data = minValue(root.right) root.right = deleteNode(root.right, root.data) if X > root.data: root.right = deleteNode(root.right, X) else: root.left = deleteNode(root.left, X) return root
FUNC_DEF IF VAR NONE RETURN NONE WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): if not root: return None if root.data == X: if not root.left and not root.right: return None if root.left and not root.right: return root.left if not root.left and root.right: return root.right ptr = root.right while ptr.left: ptr = ptr.left root.data = ptr.data root.right = deleteNode(root.right, root.data) if root.data > X: root.left = deleteNode(root.left, X) if root.data < X: root.right = deleteNode(root.right, X) return root
FUNC_DEF IF VAR RETURN NONE IF VAR VAR IF VAR VAR RETURN NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
class Node: def __init__(self, data): self.data = data self.left = None self.right = None def insert(root, key): if root is None: return Node(key) if key < root.data: root.left = insert(root.left, key) else: root.right = insert(root.right, key) return root def inorder(root): if root is not None: inorder(root.left) print(root.data, end=" ") inorder(root.right) def mini(root): if root is None: return None while root.left is not None: root = root.left return root.data def deleteNode(root, X): if root is None: return None if root.data == X: if root.left is None: return root.right elif root.right is None: return root.left else: root.data = mini(root.right) root.right = deleteNode(root.right, root.data) return root if root.data > X: root.left = deleteNode(root.left, X) else: root.right = deleteNode(root.right, X) return root
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NONE WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, key): if not root: return None def minimum(r): curr = r while curr.left: curr = curr.left return curr if key < root.data: root.left = deleteNode(root.left, key) elif key > root.data: root.right = deleteNode(root.right, key) else: if not root.right: temp = root.left return temp if not root.left: temp = root.right return temp min_node = minimum(root.right) root.data = min_node.data root.right = deleteNode(root.right, min_node.data) return root
FUNC_DEF IF VAR RETURN NONE FUNC_DEF ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
class Node: def __init__(self, key=None): self.data = key self.left = None self.right = None def getsucc(curr, key): succ = None while curr: if curr.data > key: succ = curr curr = curr.left else: curr = curr.right return succ.data if succ else None def deleteNode(root, X): if root == None: return if root.data > X: root.left = deleteNode(root.left, X) elif root.data < X: root.right = deleteNode(root.right, X) elif root.left == None: return root.right elif root.right == None: return root.left else: succ = getsucc(root.right, X) root.data = succ root.right = deleteNode(root.right, succ) return root
CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NONE WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR NONE FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): if not root: return None if X < root.data: root.left = deleteNode(root.left, X) elif X > root.data: root.right = deleteNode(root.right, X) else: if root.left is None and root.right is None: return None elif not root.left: current = root.right root = None return current elif not root.right: current = root.left root = None return current succParent = root succ = root.right while succ.left != None: succParent = succ succ = succ.left if succParent != root: succParent.left = succ.right else: succParent.right = succ.right root.data = succ.data return root
FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def ll(node): while node.right: node = node.right return node def find(curr): if curr.left is None: return curr.right elif curr.right is None: return curr.left else: left_sub = curr.left right_sub = curr.right x = ll(left_sub) x.right = right_sub return left_sub def deleteNode(root, x): curr = root if root.data == x: return find(root) if root is None: return root while curr: if x > curr.data: if curr.right and x == curr.right: curr.right = find(curr) else: curr = curr.right elif x < curr.data: if curr.left and x == curr.left: curr.left = find(curr) else: curr = curr.left return root
FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NONE RETURN VAR WHILE VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given a Binary Search Tree and a node value X. Delete the node with the given value X from the BST. If no node with value x exists, then do not make any change. Example 1: Input: 2 / \ 1 3 X = 12 Output: 1 2 3 Explanation: In the given input there is no node with value 12 , so the tree will remain same. Example 2: Input: 1 \ 2 \ 8 / \ 5 11 / \ / \ 4 7 9 12 X = 9 Output: 1 2 4 5 7 8 11 12 Explanation: In the given input tree after deleting 9 will be 1 \ 2 \ 8 / \ 5 11 / \ \ 4 7 12 Your Task: You don't need to read input or print anything. Your task is to complete the function deleteNode() which takes two arguments. The first being the root of the tree, and an integer 'X' denoting the node value to be deleted from the BST. Return the root of the BST after deleting the node with value X. Do not make any update if there's no node with value X present in the BST. Note: The generated output will be the inorder traversal of the modified tree. Expected Time Complexity: O(Height of the BST). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= N <= 10^{5}
def deleteNode(root, X): def minValue(root): minValue = root.data while root.left != None: minValue = root.left.data root = root.left return minValue if root == None: return if X < root.data: root.left = deleteNode(root.left, X) elif X > root.data: root.right = deleteNode(root.right, X) else: if root.left == None: return root.right if root.right == None: return root.left root.data = minValue(root.right) root.right = deleteNode(root.right, root.data) return root
FUNC_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: previousNode = None firstNode = None secondNode = None def correctBSTUtil(self, root): if root is None: return self.correctBSTUtil(root.left) if self.previousNode is not None and root.data < self.previousNode.data: if self.firstNode is None: self.firstNode = self.previousNode self.secondNode = root self.previousNode = root self.correctBSTUtil(root.right) def correctBST(self, root): self.correctBSTUtil(root) if self.firstNode is not None: temp = self.firstNode.data self.firstNode.data = self.secondNode.data self.secondNode.data = temp return root
CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def inorder(self, root, l): if root == None: return [] else: self.inorder(root.left, l) l.append(root.data) self.inorder(root.right, l) return l def fix(self, root, l): if root: self.fix(root.left, l) if root.data != l[0]: root.data = l[0] l.pop(0) self.fix(root.right, l) def correctBST(self, root): l = [] self.inorder(root, l) l.sort() self.fix(root, l) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): ans = [None, None, None] def inorder(nod): if nod is None: return inorder(nod.left) if ans[0] != None: if ans[1] == None: if nod.data < ans[0].data: ans[1] = ans[0] elif ans[1] != None and ans[2] is None: if nod.data > ans[0].data: ans[2] = ans[0] elif ans[2] != None and nod.data < ans[0].data: ans[2] = nod ans[0] = nod inorder(nod.right) inorder(root) if ans[2] is None: ans[0].data, ans[1].data = ans[1].data, ans[0].data else: ans[1].data, ans[2].data = ans[2].data, ans[1].data return root
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE NONE NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE IF VAR NUMBER NONE IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NONE VAR NUMBER NONE IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): temp = [] srt = [] def f(root): if not root: return None if root: f(root.left) temp.append(root) f(root.right) def values(root): if not root: return None if root: values(root.left) srt.append(root.data) values(root.right) f(root) values(root) srt = sorted(root.data for root in temp) for i in range(len(srt)): temp[i].data = srt[i]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR RETURN NONE IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN NONE IF VAR EXPR FUNC_CALL VAR VAR 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 VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def search(self, r, key, f): if r == None: return None if r.data == key: f = r return f left = self.search(r.left, key, f) if left: return left right = self.search(r.right, key, f) return right def inorder(self, root, l): if root == None: return [] else: self.inorder(root.left, l) l.append(root.data) self.inorder(root.right, l) return l def correctBST(self, root): l = [] self.inorder(root, l) t = sorted(l) for i in range(len(l)): if l[i] != t[i]: a = l[i] b = t[i] f = None g = None f = self.search(root, a, f) g = self.search(root, b, g) f.data, g.data = g.data, f.data return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): def in_(root): if root == None: return in_(root.left) if ne[0] is not None and root.data < ne[0].data: if first[0] == None: first[0] = ne[0] prev[0] = root else: sec[0] = root else: ne[0] = root in_(root.right) first = [None] sec = [None] prev = [None] ne = [None] in_(root) if first[0] and sec[0]: t = first[0].data first[0].data = sec[0].data sec[0].data = t elif first[0] and prev[0]: t = first[0].data first[0].data = prev[0].data prev[0].data = t
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR VAR NUMBER IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Pointers: def __init__(self): self.f = None self.m = None self.l = None self.p = Node(float("-inf")) class Solution: def correctBST(self, root): pointer = Pointers() def swapBST(root): if root == None: return None swapBST(root.left) if pointer.p != None and root.data < pointer.p.data: if pointer.f == None and pointer.m == None: pointer.f = pointer.p pointer.m = root else: pointer.l = root pointer.p = root swapBST(root.right) swapBST(root) if pointer.f and pointer.l: pointer.f.data, pointer.l.data = pointer.l.data, pointer.f.data else: pointer.f.data, pointer.m.data = pointer.m.data, pointer.f.data return root
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: big, small, prev = None, None, None def correctBST(self, rt): def traverse(root): if root: traverse(root.left) if self.prev and self.prev.data > root.data: self.small = root if not self.big: self.big = self.prev self.prev = root traverse(root.right) traverse(rt) self.big.data, self.small.data = self.small.data, self.big.data
CLASS_DEF ASSIGN VAR VAR VAR NONE NONE NONE FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def __init__(self): self.prev = self.first = self.second = None def findNodes(self, root): if root is None: return True self.findNodes(root.left) if self.prev is not None and root.data < self.prev.data: if self.first is None: self.first = self.prev self.second = root self.prev = root self.findNodes(root.right) def swapNodes(self, n1, n2): n1.data, n2.data = n2.data, n1.data def correctBST(self, root): self.findNodes(root) self.swapNodes(self.first, self.second)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NONE FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def inorder(self, root, prev, first, middle, last): if root is None: return self.inorder(root.left, prev, first, middle, last) if prev[0] and root.data < prev[0].data: if first[0] is None: first[0] = prev[0] middle[0] = root else: last[0] = root prev[0] = root self.inorder(root.right, prev, first, middle, last) def correctBST(self, root): prev = [None] first = [None] middle = [None] last = [None] self.inorder(root, prev, first, middle, last) if first[0] and last[0]: first[0].data, last[0].data = last[0].data, first[0].data elif first[0] and middle[0]: first[0].data, middle[0].data = middle[0].data, first[0].data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): elements = [] i = [0] def getEle(node): if node is None: return getEle(node.left) elements.append(node.data) getEle(node.right) temp = root getEle(temp) temp = root elements.sort() def setEle(node): if node is None: return setEle(node.left) node.data = elements[i[0]] i[0] += 1 setEle(node.right) setEle(temp) temp = root getEle(temp)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def inorder(self, root, arr): if root: self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) def fix_inorder(self, root, arr): if root: self.fix_inorder(root.left, arr) root.data = arr.pop(0) self.fix_inorder(root.right, arr) def correctBST(self, root): arr = list() self.inorder(root, arr) self.fix_inorder(root, sorted(arr)) return root
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBSTutil(self, root, prev, first, second, third): if not root: return self.correctBSTutil(root.left, prev, first, second, third) temp = root.data if prev[0] and not first[0]: if temp < prev[0].data: first[0] = prev[0] second[0] = root if prev[0] and first[0]: if temp < prev[0].data: third[0] = root prev[0] = root self.correctBSTutil(root.right, prev, first, second, third) def correctBST(self, root): first, second, third, prev = [None], [None], [None], [None] self.correctBSTutil(root, prev, first, second, third) if third[0]: first[0].data, third[0].data = third[0].data, first[0].data else: first[0].data, second[0].data = second[0].data, first[0].data
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR LIST NONE LIST NONE LIST NONE LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): def my(root): if not root: return my(root.left) res.append(root.data) my(root.right) def correct(root): if not root: return correct(root.left) if root.data != res2[0]: root.data = min(op) op.remove(min(op)) res2.pop(0) correct(root.right) res = [] my(root) res2 = res[:] res2.sort() op = [] for i in range(len(res)): if res[i] != res2[i]: op.append(res[i]) op.append(res2[i]) break correct(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): arr = [None, None, None, None] self.Util(root, arr) if arr[1] and arr[3]: arr[1].data, arr[3].data = arr[3].data, arr[1].data elif arr[1] and arr[2]: arr[1].data, arr[2].data = arr[2].data, arr[1].data return root def Util(self, root, arr): if not root: return self.Util(root.left, arr) if arr[0] and arr[0].data > root.data: if not arr[1]: arr[1] = arr[0] arr[2] = root else: arr[3] = root arr[0] = root self.Util(root.right, arr)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE NONE NONE NONE EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def solve(self, root, In): if root == None: return self.solve(root.left, In) In.append(root) self.solve(root.right, In) def correctBST(self, root): In = [] self.solve(root, In) i = 0 j = 1 flag = 0 f1 = None f2 = None while j < len(In): if In[i].data > In[j].data: if flag == 0: f1 = In[i] f2 = In[j] flag = 1 else: f2 = In[j] i += 1 j += 1 f1.data, f2.data = f2.data, f1.data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): s = [] cur = root while cur: s.append(cur) cur = cur.left prev = first = second = None while s: elem = s.pop() if prev and prev.data > elem.data: if not first: first = prev second = elem prev = elem cur = elem.right while cur: s.append(cur) cur = cur.left first.data, second.data = second.data, first.data
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NONE WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): def getNumbers(root): nonlocal firstNode, secondNode, prev if not root: return getNumbers(root.left) if prev and prev.data > root.data: if not firstNode: firstNode = prev secondNode = root prev = root getNumbers(root.right) firstNode = secondNode = prev = None getNumbers(root) temp = firstNode.data firstNode.data = secondNode.data secondNode.data = temp return root
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: prev = None first = None second = None def correctBST(self, root): self.first, self.second = self._correctBST(root) self.first.data, self.second.data = self.second.data, self.first.data def _correctBST(self, root): if root == None: return self._correctBST(root.left) if self.prev is not None and self.prev.data > root.data: if self.first is None: self.first = self.prev self.second = root self.prev = root self._correctBST(root.right) return self.first, self.second
CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def check(self, first, second, root, pre): if first[0] == None and root.data < pre.data: first[0] = pre if first[0] and root.data < pre.data: second[0] = root def correctBST(self, root): if root.left == None and not root.right: return current = root predecessor = Node(-float("inf")) first_mistake = [None] second_mistake = [None] while current: if current.left == None: self.check(first_mistake, second_mistake, current, predecessor) predecessor = current current = current.right else: left = current.left while left.right and left.right != current: left = left.right if left.right == None: left.right = current current = current.left else: left.right = None self.check(first_mistake, second_mistake, current, predecessor) predecessor = current current = current.right first_mistake[0].data, second_mistake[0].data = ( second_mistake[0].data, first_mistake[0].data, )
CLASS_DEF FUNC_DEF IF VAR NUMBER NONE VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_DEF IF VAR NONE VAR RETURN ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE WHILE VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): arr = [] def inorder(root): if not root: return inorder(root.left) arr.append(root.data) inorder(root.right) return nodes = [None, None] def find(root, ele1, ele2): if not root: return if root.data == ele1: nodes[0] = root elif root.data == ele2: nodes[1] = root find(root.left, ele1, ele2) find(root.right, ele1, ele2) return inorder(root) n = len(arr) fault = -1 for i in range(n - 1): if arr[i] > arr[i + 1]: fault = i break ele1 = arr[fault] ele2 = 0 second = -1 for j in range(fault + 1, n): if arr[j] > ele1: second = j - 1 break ele2 = arr[second] small = min(arr) - 1 find(root, ele1, ele2) nodes[0].data, nodes[1].data = nodes[1].data, nodes[0].data return
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 RETURN ASSIGN VAR LIST NONE NONE FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): prev = None first = None second = None stack = [] while stack or root: while root: stack.append(root) root = root.left root = stack.pop() if prev and prev.data > root.data: if not first: first = prev second = root prev = root root = root.right first.data, second.data = second.data, first.data
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): res = [] def help(r): if r: help(r.left) res.append(r) help(r.right) help(root) n = len(res) - 1 i, j = 0, n while i <= n and res[i].data < res[i + 1].data: i += 1 while j > 1 and res[j].data > res[j - 1].data: j -= 1 res[i].data, res[j].data = res[j].data, res[i].data
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): self.l = None self.r = None self.mr = None self.bst(root) l = self.l.data r = self.r.data self.l.data = r self.r.data = l return root def bst(self, root): if root == None: return self.bst(root.left) if self.mr != None and root.data < self.mr.data: if self.l == None: self.l = self.mr self.r = root self.mr = root self.bst(root.right)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class NodesInfo: def __init__(self): self.prev = Node(float("-inf")) self.first = None self.second = None class Solution: def correctBST(self, root): nodesInfo = NodesInfo() self.correctBSTUtil(root, nodesInfo) nodesInfo.first.data, nodesInfo.second.data = ( nodesInfo.second.data, nodesInfo.first.data, ) def correctBSTUtil(self, root, nodesInfo): if root is None: return self.correctBSTUtil(root.left, nodesInfo) if root.data < nodesInfo.prev.data: if nodesInfo.first is None: nodesInfo.first = nodesInfo.prev nodesInfo.second = root nodesInfo.prev = root self.correctBSTUtil(root.right, nodesInfo)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def ino(self, root): ans = [] if root: ans += self.ino(root.left) ans.append(root.data) ans += self.ino(root.right) return ans def correctBST(self, root): l = self.ino(root) l.sort() def update(root): if root: update(root.left) root.data = l[0] l.pop(0) update(root.right) update(root) return root
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): curr = root pre, a, b = None, None, None while curr is not None: left = curr.left if left is None: if pre is not None and pre.data > curr.data: if a is None: a = pre b = curr pre = curr curr = curr.right else: rightMost = self.getRightMostNode(left, curr) if rightMost.right is None: rightMost.right = curr curr = curr.left else: rightMost.right = None if pre is not None and pre.data > curr.data: if a is None: a = pre b = curr pre = curr curr = curr.right if a is not None: a.data, b.data = b.data, a.data return root def getRightMostNode(self, l, c): while l.right is not None and l.right != c: l = l.right return l
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR NONE NONE NONE WHILE VAR NONE ASSIGN VAR VAR IF VAR NONE IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF WHILE VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): a, b = None, None prev = Node(float("-inf")) def inorder(root): nonlocal a, b, prev if root: inorder(root.left) if root.data < prev.data: if not a: a = prev b = root prev = root inorder(root.right) inorder(root) a.data, b.data = b.data, a.data
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): first = second = None sort_arr = [] def traverse(node): if node.left: traverse(node.left) sort_arr.append(node) if node.right: traverse(node.right) traverse(root) for i in range(1, len(sort_arr)): if sort_arr[i].data > sort_arr[i - 1].data: continue elif first != None: second = i else: first = i - 1 second = i sort_arr[first].data, sort_arr[second].data = ( sort_arr[second].data, sort_arr[first].data, ) return root
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
import sys class Solution: def correctBST(self, root): if not root: return None prev = Node(-sys.maxsize) first = None second = None curr = root while curr: if curr.left == None: if prev.data > curr.data: if first == None: first = prev second = curr else: second = curr prev = curr curr = curr.right else: rootrightmost = curr.left while rootrightmost.right and rootrightmost.right != curr: rootrightmost = rootrightmost.right if rootrightmost.right == None: rootrightmost.right = curr curr = curr.left else: rootrightmost.right = None if prev.data > curr.data: if first == None: first = prev second = curr else: second = curr prev = curr curr = curr.right first.data, second.data = second.data, first.data
IMPORT CLASS_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR IF VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): def inorder(root): if not root: return inorder(root.left) arr.append(root.data) inorder(root.right) def update(node, arr): if not node: return update(node.left, arr) node.data = arr.pop(len(arr) - 1) update(node.right, arr) arr = [] inorder(root) arr = sorted(arr, reverse=True) update(root, arr)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): def inorder(root): if root == None: return inorder(root.left) if self.prev and root.data < self.prev.data: if self.left: self.first = self.prev self.second = root self.left = 0 else: self.last = root self.prev = root inorder(root.right) self.left = 1 self.first = self.second = self.last = -1 self.prev = Node(float("-inf")) inorder(root) if self.last == -1: self.first.data, self.second.data = (self.second.data, self.first.data) else: self.first.data, self.last.data = self.last.data, self.first.data
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def inorder(self, root, A, __map): if not root: return __map[root.data] = root self.inorder(root.left, A, __map) A.append(root.data) self.inorder(root.right, A, __map) def correctBST(self, root): A = [] __Map = {} self.inorder(root, A, __Map) sA = A.copy() sA.sort() for i in range(len(A)): if A[i] != sA[i]: __Map[A[i]].data = sA[i] __Map[sA[i]].data = A[i] break
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): first = [None] mid = [None] last = [None] pre = [None] def ino(r): if not r: return ino(r.left) if pre[0] != None and r.data < pre[0].data: if first[0] == None: first[0] = pre[0] mid[0] = r else: last[0] = r pre[0] = r ino(r.right) ino(root) if first[0] != None and last[0] == None: t = first[0].data first[0].data = mid[0].data mid[0].data = t elif first[0] != None and last[0] != None: t = first[0].data first[0].data = last[0].data last[0].data = t
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR VAR NUMBER IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def solve(self, root, q, t): if root is None: return self.solve(root.left, q, t) t.append(root) q.append(root.data) self.solve(root.right, q, t) def correctBST(self, root): t = [] q = [] j = [] self.solve(root, q, t) for i in range(len(q) - 1): if q[i] > q[i + 1]: j.append(i) j.append(i + 1) t[j[0]].data, t[j[len(j) - 1]].data = t[j[len(j) - 1]].data, t[j[0]].data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): curr = root first = None mid = None last = None inorder = None while curr: if curr.left == None: if inorder != None and inorder.data > curr.data and first == None: first = inorder mid = curr elif first != None and inorder.data > curr.data: last = curr inorder = curr curr = curr.right else: prev = curr.left while prev.right != None and prev.right != curr: prev = prev.right if prev.right == None: prev.right = curr curr = curr.left else: prev.right = None if inorder != None and inorder.data > curr.data and first == None: first = inorder mid = curr elif first != None and inorder.data > curr.data: last = curr inorder = curr curr = curr.right if last == None: first.data, mid.data = mid.data, first.data else: first.data, last.data = last.data, first.data
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR IF VAR NONE IF VAR NONE VAR VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR NONE VAR VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): ans = [] def find(root): if root: find(root.left) ans.append(root) find(root.right) return find(root) a = [] for i in range(1, len(ans)): if ans[i - 1].data > ans[i].data: if not a: a.append(ans[i]) a.append(ans[i - 1]) else: a[0] = ans[i] a[0].data, a[1].data = a[1].data, a[0].data
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): if root == None: return self.first, self.mid, self.second, self.prev = None, None, None, None def inorder(root): if root == None: return inorder(root.left) if self.prev and self.prev.data > root.data: if self.first: self.second = root else: self.first = self.prev self.mid = root self.prev = root inorder(root.right) inorder(root) if self.second: self.first.data, self.second.data = (self.second.data, self.first.data) else: self.first.data, self.mid.data = self.mid.data, self.first.data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR VAR VAR NONE NONE NONE NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): global list1 list1 = [] self.inOrder(root) sorted_list = sorted(list1, key=lambda x: x.data) for i in range(len(list1)): if sorted_list[i] != list1[i]: sorted_list[i].data, list1[i].data = list1[i].data, sorted_list[i].data return def inOrder(self, root): global list1 if root is None: return self.inOrder(root.left) list1.append(root) self.inOrder(root.right)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): self.temp = [] def dfs(node): if not node: return dfs(node.left) self.temp.append(node) dfs(node.right) dfs(root) srt = sorted(n.data for n in self.temp) for i in range(len(srt)): self.temp[i].data = srt[i]
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 VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): if not (root.left or root.right): return root prev, first, second = [None], [None], [None] self.rec(root, prev, first, second) first[0].data, second[0].data = second[0].data, first[0].data return root def rec(self, root, prev, first, second): if not root: return self.rec(root.left, prev, first, second) if prev[0] and prev[0].data > root.data: if first[0] is None: first[0] = prev[0] second[0] = root prev[0] = root self.rec(root.right, prev, first, second)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR LIST NONE LIST NONE LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): first = [None] middle = [None] last = [None] prev = [None] self.correctBSTUtil(root, first, middle, last, prev) if first[0] and last[0]: first[0].data, last[0].data = last[0].data, first[0].data elif first[0] and middle[0]: first[0].data, middle[0].data = middle[0].data, first[0].data def correctBSTUtil(self, root, first, middle, last, prev): if root: self.correctBSTUtil(root.left, first, middle, last, prev) if prev[0] and prev[0].data > root.data: if not first[0]: first[0] = prev[0] middle[0] = root else: last[0] = root prev[0] = root self.correctBSTUtil(root.right, first, middle, last, prev)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def inorder(self, root): if root is None: return self.inorder(root.left) if self.prev is None: self.prev = root else: if self.prev.data > root.data: self.possibles.append(self.prev) self.possibles.append(root) self.prev = root if len(self.possibles) == 4: return self.inorder(root.right) def correctBST(self, root): self.prev = None self.possibles = [] self.inorder(root) node1 = self.possibles[0] node2 = self.possibles[-1] node1.data, node2.data = node2.data, node1.data return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): arr = [] def traverse(root): if root == None: return traverse(root.left) arr.append(root.data) traverse(root.right) traverse(root) index = 0 arr = sorted(arr) self.index = 0 def modify(root): if root == None: return modify(root.left) root.data = arr[self.index] self.index += 1 modify(root.right) modify(root)
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 EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): arr = [] def inorder(root): nonlocal arr if root != None: inorder(root.left) arr.append(root) inorder(root.right) inorder(root) n = len(arr) f_index = 0 s_index = n - 1 for i in range(1, n): if arr[i - 1].data > arr[i].data: f_index = i - 1 break for j in range(n - 2, -1, -1): if arr[j].data > arr[j + 1].data: s_index = j + 1 break arr[f_index].data, arr[s_index].data = arr[s_index].data, arr[f_index].data
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 VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): arr = [] def inorder(root): if not root: return inorder(root.left) arr.append(root.data) inorder(root.right) return def change(root, ch, ele): if not root: return False if root.data == ele: root.data = ch return True l = change(root.left, ch, ele) if l: return True r = change(root.right, ch, ele) if r: return True return False inorder(root) n = len(arr) fault = -1 for i in range(n - 1): if arr[i] > arr[i + 1]: fault = i break ele1 = arr[fault] ele2 = 0 second = -1 for j in range(fault + 1, n): if arr[j] > ele1: second = j - 1 break ele2 = arr[second] small = min(arr) - 1 change(root, small, ele1) change(root, ele1, ele2) change(root, ele2, small) return
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 RETURN FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): self.swapped = [None, None] self.prev = Node(float("-inf")) def inorder(node): if node is None: return inorder(node.left) if self.swapped[0] is None and self.prev.data >= node.data: self.swapped[0] = self.prev if self.swapped[0] is not None and self.prev.data >= node.data: self.swapped[1] = node self.prev = node inorder(node.right) inorder(root) self.swapped[0].data, self.swapped[1].data = ( self.swapped[1].data, self.swapped[0].data, )
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER NONE VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def correctBST(self, root): v = [] self.inorder(root, v) v.sort() self.check(root, v) def inorder(self, root, v): if root == None: return self.inorder(root.left, v) v.append(root.data) self.inorder(root.right, v) def check(self, root, v): if root == None: return self.check(root.left, v) if v[0] != root.data: root.data = v[0] v.pop(0) self.check(root.right, v)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given the root of a binary search tree(BST), where exactly two nodes were swapped by mistake. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree. Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong. All changes must be reflected in the original linked list. Example 1: Input: 10 / \ 5 8 / \ 2 20 Output: 1 Explanation: Example 2: Input: 11 / \ 3 17 \ / 4 10 Output: 1 Explanation: By swapping nodes 11 and 10, the BST can be fixed. Your Task: You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should not return anything, all the changes must be done in the existing tree only. BST will then be checked by driver code and 0 or 1 will be printed. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ Number of nodes ≤ 10^{3}
class Solution: def getInorder(self, root): if root is None: return self.getInorder(root.left) self.inorder.append(root.data) self.getInorder(root.right) def correctUtil(self, root, swap_1, swap_2): if root is None: return self.correctUtil(root.left, swap_1, swap_2) if root.data in [swap_1, swap_2]: root.data ^= swap_1 ^ swap_2 self.correctUtil(root.right, swap_1, swap_2) return def correctBST(self, root): self.inorder = [] self.getInorder(root) swap_1, swap_2 = -1, -1 for i in range(len(self.inorder) - 1): if self.inorder[i] > self.inorder[i + 1]: if swap_1 == -1: swap_1 = self.inorder[i] swap_2 = self.inorder[i + 1] else: swap_2 = self.inorder[i + 1] self.correctUtil(root, swap_1, swap_2)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR IF VAR LIST VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def convertToMaxHeapUtil(self, root): def reverseInorder(root): if not root: return reverseInorder(root.right) arr.append(root.data) reverseInorder(root.left) def convertBSTToMaxHeap(root): nonlocal index if not root: return root.data = arr[index] index += 1 convertBSTToMaxHeap(root.right) convertBSTToMaxHeap(root.left) arr = [] index = 0 reverseInorder(root) convertBSTToMaxHeap(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def inorder(self, root, c): if root: self.inorder(root.left, c) c.append(root.data) self.inorder(root.right, c) def postorder(self, root, c): if root: self.postorder(root.left, c) self.postorder(root.right, c) root.data = c[self.i] self.i += 1 def convertToMaxHeapUtil(self, root): c = [] p = [] self.i = 0 self.inorder(root, c) self.postorder(root, c) return c
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def convertToMaxHeapUtil(self, root): global i i = 0 arr = [] def inOrder(root): if root == None: return arr inOrder(root.left) arr.append(root.data) inOrder(root.right) def bstToHeap(root): global i if root == None: return None root.left = bstToHeap(root.left) root.right = bstToHeap(root.right) root.data = arr[i] i += 1 return root inOrder(root) root = bstToHeap(root) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def postorder(self, root, brr): if root == None: return self.postorder(root.left, brr) self.postorder(root.right, brr) brr.append(root) def inorder(self, root, arr): if root == None: return self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) def convertToMaxHeapUtil(self, root): arr = [] brr = [] curr = root self.inorder(curr, arr) curr = root self.postorder(curr, brr) for i in range(len(arr)): brr[i].data = arr[i] return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def inOrder(self, root, arr): if root is None: return arr self.inOrder(root.left, arr) arr.append(root.data) self.inOrder(root.right, arr) def postOrder(self, root, arr2, index): if root is None: return self.postOrder(root.left, arr2, index) self.postOrder(root.right, arr2, index) root.data = arr2[index[0]] index[0] += 1 def convertToMaxHeapUtil(self, root): arr2 = [] self.inOrder(root, arr2) index = [0] * 1 self.postOrder(root, arr2, index)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
class Solution: def __init__(self): self.A = [] self.ind = 0 def buildTree(self, root, A, ind): if root == None: return self.buildTree(root.left, A, ind) self.buildTree(root.right, A, ind) root.data = self.A[self.ind] self.ind += 1 def inorder(self, root, A): if root == None: return self.inorder(root.left, A) A.append(root.data) self.inorder(root.right, A) def convertToMaxHeapUtil(self, root): self.inorder(root, self.A) self.buildTree(root, self.A, self.ind)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree. Convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied on all the nodes in the so converted Max Heap. Example 1: Input : 4 / \ 2 6 / \ / \ 1 3 5 7 Output : 1 2 3 4 5 6 7 Exaplanation : 7 / \ 3 6 / \ / \ 1 2 4 5 The given BST has been transformed into a Max Heap and it's postorder traversal is 1 2 3 4 5 6 7. Your task : You don't need to read input or print anything. Your task is to complete the function convertToMaxHeapUtil() which takes the root of the tree as input and converts the BST to max heap. Note : The driver code prints the postorder traversal of the converted BST. Expected Time Complexity : O(n) Expected Auxiliary Space : O(n) Constraints : 1 ≤ n ≤ 10^{5}
def util(arr): if not arr: return None root = Node(arr[-1]) arr = arr[:-1] n = len(arr) root.left = util(arr[: n // 2]) root.right = util(arr[n // 2 :]) return root def inorder(root, arr): if not root: return inorder(root.left, arr) arr.append(root.data) inorder(root.right, arr) def preorder(root, arr): if not root: return arr.append(root.data) preorder(root.left, arr) preorder(root.right, arr) class Solution: def convertToMaxHeapUtil(self, root): arr = [] inorder(root, arr) troot = util(arr) root.data = troot.data root.left = troot.left root.right = troot.right
FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR