description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
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):
arr = []
def dfs1(root):
if not root:
return
dfs1(root.right)
arr.append(root.data)
dfs1(root.left)
dfs1(root)
i = 0
def dfs2(root):
nonlocal i
if not root:
return
root.data = arr[i]
i += 1
dfs2(root.right)
dfs2(root.left)
dfs2(root) | 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 NUMBER FUNC_DEF IF VAR RETURN ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR 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 convertToMaxHeapUtil(self, root):
self.i = 0
def manu(root, a):
if root is None:
return
manu(root.left, a)
a.append(root.data)
manu(root.right, a)
a = []
manu(root, a)
def postorder(root, i, a):
if root is None:
return
root.left = postorder(root.left, i, a)
root.right = postorder(root.right, i, a)
root.data = a[i[0]]
i[0] += 1
return root
postorder(root, [0], a) | CLASS_DEF FUNC_DEF ASSIGN 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 ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR LIST NUMBER 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, lis):
if root == None:
return
self.inorder(root.left, lis)
lis.append(root.data)
self.inorder(root.right, lis)
def solve(self, root, lis):
if root == None:
return
self.solve(root.left, lis)
self.solve(root.right, lis)
root.data = lis.pop()
def convertToMaxHeapUtil(self, root):
lis = []
self.inorder(root, lis)
lis = lis[::-1]
self.solve(root, lis)
return root | 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 IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER 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):
def inorderTraversal(root, arr):
if root == None:
return
inorderTraversal(root.left, arr)
arr.append(root.data)
inorderTraversal(root.right, arr)
def BSTtoMaxHeap(root, arr, i):
if root == None:
return
BSTtoMaxHeap(root.left, arr, i)
BSTtoMaxHeap(root.right, arr, i)
i[0] += 1
root.data = arr[i[0]]
arr = []
i = [-1]
inorderTraversal(root, arr)
BSTtoMaxHeap(root, arr, i) | CLASS_DEF FUNC_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 IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER 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} | class Solution:
def convertToMaxHeapUtil(self, root):
v = []
def inorder(x):
if x:
inorder(x.left)
v.append(x.data)
inorder(x.right)
def preorder(x):
if x:
preorder(x.left)
preorder(x.right)
x.data = v[i[0]]
i[0] += 1
inorder(root)
i = [0]
preorder(root)
return root | 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 FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR 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} | def innorder(root):
if not root:
return []
return innorder(root.left) + [root.data] + innorder(root.right)
def BSTToMaxHeap(root, arr):
global i
if root == None:
return None
root.left = BSTToMaxHeap(root.left, arr)
root.right = BSTToMaxHeap(root.right, arr)
root.data = arr[i]
i = i + 1
return root
class Solution:
def convertToMaxHeapUtil(self, root):
arr = innorder(root)
global i
i = 0
return BSTToMaxHeap(root, arr) | FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL 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:
arr, index = [], 0
def inorder(self, root):
if root != None:
self.inorder(root.left)
self.arr.append(root.data)
self.inorder(root.right)
def filltree(self, root):
if root == None:
return
self.filltree(root.left)
self.filltree(root.right)
root.data = self.arr[self.index]
self.index += 1
def convertToMaxHeapUtil(self, root):
self.arr, self.index = [], 0
self.inorder(root)
self.filltree(root) | CLASS_DEF ASSIGN VAR VAR LIST NUMBER FUNC_DEF IF VAR NONE 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR LIST 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 heapify(self, root, temp):
if root is None:
return
if root.left is None and root.right is None:
return
largest = root
if root.left:
if root.data < root.left.data:
largest = root.left
if root.right:
if root.data < root.right.data:
largest = root.right
if largest != root:
root, largest = largest, root
self.heapify(largest, root)
else:
self.heapify(root, largest)
def ino_post(self, root, s):
if root is None:
return
if root:
self.ino_post(root.left, s)
s.append(root.data)
self.ino_post(root.right, s)
root.data = s[self.cnt]
self.cnt += 1
def convertToMaxHeapUtil(self, root):
s = []
self.cnt = 0
self.ino_post(root, s) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE RETURN ASSIGN VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL 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 inOrder(self, root):
cur = root
stack = []
ans = []
while 1:
if cur is not None:
stack.append(cur)
cur = cur.left
elif stack:
cur = stack.pop()
ans.append(cur.data)
cur = cur.right
else:
break
return ans
def BSTToMaxHeap(self, root, arr):
if root == None:
return None
root.left = self.BSTToMaxHeap(root.left, arr)
root.right = self.BSTToMaxHeap(root.right, arr)
root.data = arr.pop()
return root
def convertToMaxHeapUtil(self, root):
ans = self.inOrder(root)
ans.reverse()
return self.BSTToMaxHeap(root, ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL 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 getArr(self, root, ls):
if not root:
return
self.getArr(root.left, ls)
ls.append(root.data)
self.getArr(root.right, ls)
def generateTree(self, ls, idx, root):
if not root or idx[0] == len(ls):
return
self.generateTree(ls, idx, root.left)
self.generateTree(ls, idx, root.right)
idx[0] += 1
root.data = ls[idx[0]]
def convertToMaxHeapUtil(self, root):
ls = []
self.getArr(root, ls)
n = len(s)
i = [-1]
self.generateTree(ls, i, root) | CLASS_DEF 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 VAR NUMBER FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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):
l = []
def dfs(root, l):
if root == None:
return
dfs(root.left, l)
l.append(root.data)
dfs(root.right, l)
i = [-1]
dfs(root, l)
def maxh(root, i):
if root == None:
return
maxh(root.left, i)
maxh(root.right, i)
i[0] = i[0] + 1
root.data = l[i[0]]
maxh(root, i)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER 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 preorder(self, root, arr):
if root == None:
return
self.preorder(root.left, arr)
arr.append(root.data)
self.preorder(root.right, arr)
def convert(self, root, arr):
if root == None:
return
root.left = self.convert(root.left, arr)
root.right = self.convert(root.right, arr)
root.data = arr[self.i]
self.i += 1
return root
def convertToMaxHeapUtil(self, root):
arr = []
self.preorder(root, arr)
self.i = 0
root = self.convert(root, arr)
return root | 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 IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR 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} | def inorder(node, A):
if node is None:
return
inorder(node.left, A)
A.append(node.data)
inorder(node.right, A)
def postorder(node, A, index):
if node is None:
return
postorder(node.left, A, index)
postorder(node.right, A, index)
node.data = A[index[0]]
index[0] += 1
return
class Solution:
def convertToMaxHeapUtil(self, root):
index = [0]
A = []
inorder(root, A)
postorder(root, A, index)
return root | 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 VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER RETURN CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL 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 convertToMaxHeapUtil(self, root):
st = deque()
nodes = []
current = root
while current:
if current.left:
left = current.left
while left.right and left.right != current:
left = left.right
if left.right == None:
left.right = current
current = current.left
if left.right == current:
left.right = None
nodes.append(current.data)
current = current.right
else:
nodes.append(current.data)
current = current.right
current = root
i = 0
while 1:
while current:
st.append(current)
st.append(current)
current = current.left
if not st:
break
nd = st.pop()
if st and st[-1] == nd:
current = nd.right
else:
nd.data = nodes[i]
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
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):
inord = []
def inorder(root):
nonlocal inord
if not root:
return
else:
inorder(root.left)
inord.append(root.data)
inorder(root.right)
inorder(root)
def postorder(root, arr):
nonlocal inord
if not root:
return
else:
postorder(root.left, arr)
postorder(root.right, arr)
root.data = inord[arr[0]]
arr[0] += 1
postorder(root, [0])
return root | 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 FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST NUMBER 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):
def rajeev(root, a):
if root == None:
return a
a = a + rajeev(root.right, a) + [root.data] + rajeev(root.left, a)
return a
s = rajeev(root, [])
def funk(root, a, i, n):
if root == None:
return i
root.data = a[i]
i += 1
i = funk(root.right, a, i, n)
return funk(root.left, a, i, n)
funk(root, s, 0, len(s)) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR LIST VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR LIST FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER 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} | def inOrder(root):
if root == None:
return []
return inOrder(root.left) + [root.data] + inOrder(root.right)
def convert(arr, pos, n, k):
if k >= n:
return pos, None
nptr = Node(arr[pos])
pos, nptr.right = convert(arr, pos - 1, n, 2 * k + 2)
pos, nptr.left = convert(arr, pos, n, 2 * k + 1)
return pos, nptr
class Solution:
def convertToMaxHeapUtil(self, root):
arr = inOrder(root)
n = len(arr)
nptr = convert(arr, n - 1, n, 0)[1]
root.data = nptr.data
root.left, root.right = nptr.left, nptr.right | FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN 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 inorder(root, l):
if root:
inorder(root.left, l)
l.append(root.data)
inorder(root.right, l)
def order(root, l):
if root:
order(root.left, l)
order(root.right, l)
root.data = l[-1]
l.pop(-1)
l = []
inorder(root, l)
l = l[::-1]
order(root, l) | CLASS_DEF FUNC_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 NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL 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):
lis = []
self.inorder(root, lis)
lis = lis[::-1]
self.postorder(root, lis)
return root
def inorder(self, rt, li):
if rt is None:
return
self.inorder(rt.left, li)
li.append(rt.data)
self.inorder(rt.right, li)
def postorder(self, rt, li):
if rt is None:
return
self.postorder(rt.left, li)
self.postorder(rt.right, li)
rt.data = li.pop() | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL 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:
a = []
i = 0
def postt(self, r):
if r == None:
return
self.postt(r.left)
self.postt(r.right)
r.data = Solution.a[Solution.i]
Solution.i += 1
def pret(self, r):
if r == None:
return
self.pret(r.left)
Solution.a.append(r.data)
self.pret(r.right)
def convertToMaxHeapUtil(self, root):
Solution.a = []
Solution.i = 0
self.pret(root)
self.postt(root) | CLASS_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF 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_trav(self, cur_node, inorder):
if cur_node == None:
return None
self.inorder_trav(cur_node.left, inorder)
inorder.append(cur_node.data)
self.inorder_trav(cur_node.right, inorder)
def convert_heap(self, inorder, l, r):
if l > r:
return
mid = (l + r - 1) // 2
root = Node(inorder[r])
root.left = self.convert_heap(inorder, l, mid)
root.right = self.convert_heap(inorder, mid + 1, r - 1)
return root
def convertToMaxHeapUtil(self, root):
inorder = []
self.inorder_trav(root, inorder)
heap_root = self.convert_heap(inorder, 0, len(inorder) - 1)
root.data = heap_root.data
root.left = heap_root.left
root.right = heap_root.right | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN 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, arr):
if root == None:
return None
self.Inorder(root.left, arr)
arr.append(root.data)
self.Inorder(root.right, arr)
def post(self, root, arr):
if root == None:
return None
self.post(root.left, arr)
self.post(root.right, arr)
root.data = arr[-1]
arr.pop(-1)
def convertToMaxHeapUtil(self, root):
arr = []
self.Inorder(root, arr)
arr.reverse()
self.post(root, arr) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL 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):
arr = []
i = 0
def ino(node):
if not node:
return
ino(node.left)
arr.append(node.data)
ino(node.right)
ino(root)
def dfs(node):
nonlocal i
if not node:
return
dfs(node.left)
dfs(node.right)
node.data = arr[i]
i += 1
dfs(root) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER 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 FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER 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 convertToMaxHeapUtil(self, root):
def helper(root):
if root == None:
return []
return helper(root.left) + [root.data] + helper(root.right)
inorder = helper(root)
i = 0
def postorder(root):
nonlocal i
if root == None:
return
postorder(root.left)
postorder(root.right)
root.data = inorder[i]
i += 1
postorder(root)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR 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 convertToMaxHeapUtil(self, root):
arr = self.getItems(root)
stack = [root]
while stack:
head = stack.pop()
if head == None:
continue
head.data = arr.pop()
stack.append(head.left)
stack.append(head.right)
return
def getItems(self, root):
if root is None:
return []
output = []
output += self.getItems(root.left)
output += [root.data]
output += self.getItems(root.right)
return output | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR LIST VAR 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 convertToMaxHeapUtil(self, roota):
global root
ret = self.inOrder(roota, [])
root = self.buildHeap(ret)
return root
def buildHeap(self, ret):
parent = ret[len(ret) - 1]
left_index = len(ret) // 2 - 1
left = ret[left_index]
right = ret[len(ret) - 2]
if parent is not None and left.data != parent.data:
parent.left = left
if parent.left.data != right.data:
parent.right = right
if parent.left is not None:
parent.left.left = None
parent.left.right = None
if parent.right is not None:
parent.right.left = None
parent.right.right = None
if left_index > 0:
self.buildHeap(ret[: left_index + 1])
self.buildHeap(ret[left_index + 1 : len(ret) - 1])
return parent
def inOrder(self, root, ret):
if root is None:
return []
self.inOrder(root.left, ret)
if root is not None:
ret.append(root)
self.inOrder(root.right, ret)
return ret | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE IF VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL 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):
arr = []
def inorder(node):
if not node:
return None
inorder(node.left)
arr.append(node.data)
inorder(node.right)
def postOrder(node):
if not node:
return None
postOrder(node.left)
postOrder(node.right)
node.data = arr.pop()
inorder(root)
arr.reverse()
postOrder(root)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if k == 0:
j = 0
for i in range(0, len(nums)):
if nums[i] == 0:
if j < i:
return True
else:
j = i + 1
return False
dic = {(0): -1}
c = 0
for i in range(0, len(nums)):
c = (c + nums[i]) % k
if c in dic:
if i - dic[c] > 1:
return True
else:
dic[c] = i
return False | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if not nums or len(nums) < 2:
return False
k = abs(k)
n = len(nums)
d = {(0): -1}
sum = 0
for i in range(n):
sum += nums[i]
key = sum % k if k > 0 else sum
if key not in d:
d[key] = i
elif i >= d[key] + 2:
print(d[key] + 1, i)
return True
return False | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution(object):
def checkSubarraySum(self, nums, k):
if k == 0:
for i in range(0, len(nums) - 1):
if nums[i] == 0 and nums[i + 1] == 0:
return True
return False
k = abs(k)
if len(nums) >= k * 2:
return True
sum = [0]
for x in nums:
sum.append((sum[-1] + x) % k)
Dict = {}
for i in range(0, len(sum)):
if sum[i] in Dict:
if i - Dict[sum[i]] > 1:
return True
else:
Dict[sum[i]] = i
return False | CLASS_DEF VAR FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if k == 0:
for i in range(len(nums) - 1):
if nums[i] == nums[i + 1] == 0:
return True
return False
if len(nums) <= 1:
return False
s = 0
acc = []
for n in nums:
s += n
s %= k
acc.append(s)
for i in range(1, len(nums)):
if acc[i] == 0:
return True
return len(set(acc)) != len(acc) | CLASS_DEF FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
P = [0]
for x in nums:
v = P[-1] + x
if k:
v %= abs(k)
P.append(v)
seen = set()
for i in range(len(P) - 3, -1, -1):
seen.add(P[i + 2])
if P[i] in seen:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
dict, cur = {}, 0
if k == 0:
zero = 0
for i in nums:
if i == 0 and zero == 1:
return True
elif i == 0 and zero != 1:
zero = 1
else:
zero = 0
return False
for i in range(len(nums)):
cur += nums[i]
if i != 0 and cur % k == 0:
print("1")
return True
elif cur % k in dict and cur >= k:
print("2")
return True
elif i != 0 and nums[i] == 0 and nums[i - 1] == 0:
print("3")
return True
else:
dict[cur % k] = True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if not nums or len(nums) <= 1:
return False
if k == 0:
for i in range(1, len(nums)):
if nums[i] == 0 and nums[i - 1] == 0:
return True
else:
nums[0] = nums[0] % k
for i in range(1, len(nums)):
nums[i] = (nums[i - 1] + nums[i]) % k
if nums[i] == 0:
return True
numdict = {}
for i in range(len(nums)):
if nums[i] not in numdict:
numdict[nums[i]] = i
elif i > numdict[nums[i]] + 1:
return True
return False | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
indexs = {}
indexs[0] = -1
curr_sum = 0
for i in range(len(nums)):
if k != 0:
curr_sum += nums[i]
modv = curr_sum % k
if modv in indexs and i - indexs[modv] > 1:
return True
if not modv in indexs:
indexs[modv] = i
elif nums[i] == 0 and i < len(nums) - 1 and nums[i + 1] == 0:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
sum = 0
indices = {(0): -1}
for i in range(len(nums)):
sum += nums[i]
if k == 0:
l = indices.get(sum)
if l is not None and i - l >= 2:
return True
elif l is None:
indices[sum] = i
else:
l = indices.get(sum % k)
if l is not None and i - l >= 2:
return True
elif l is None:
indices[sum % k] = i
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if not k:
i = 0
while i < len(nums) - 1:
if not nums[i] and not nums[i + 1]:
return True
i += 1
return False
k = abs(k)
dic = {(0): -1}
rem = 0
i = 0
while i < len(nums):
rem = (rem + nums[i]) % k
if rem not in dic:
dic[rem] = i
elif dic[rem] < i - 1:
return True
i += 1
return False | CLASS_DEF FUNC_DEF IF VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
s = 0
dic = collections.defaultdict(list)
dic[0].append(-1)
for i in range(0, len(nums)):
s += nums[i]
if k == 0:
dic[s].append(i)
if i - dic[s][0] > 1:
return True
else:
dic[s % k].append(i)
if i - dic[s % k][0] > 1:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if not nums or len(nums) < 2:
return False
if k == 1:
return True
cur_sum, sums = 0, {(0): -1}
for i, x in enumerate(nums):
cur_sum += x
mod = cur_sum % k if k != 0 else cur_sum
if mod in sums:
if i - sums[mod] >= 2:
return True
else:
sums[mod] = i
return False | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER |
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer. | class Solution:
def checkSubarraySum(self, nums, k):
if k == 0:
i = 1
while i < len(nums):
if nums[i - 1] == 0 and nums[i] == 0:
return True
i += 1
return False
d = dict()
d[0] = -1
s = 0
for i, x in enumerate(nums):
s += x
s = s % k
if s in d:
if i - d[s] > 1:
return True
else:
d[s] = i
return False | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
d = {}
q = deque()
q.append([root, None])
house = None
while len(q) > 0:
t = q.popleft()
if t[0].data == home:
house = t[0]
if t[0] not in d:
d[t[0]] = [t[1], t[0].left, t[0].right]
if t[0].left:
q.append([t[0].left, t[0]])
if t[0].right:
q.append([t[0].right, t[0]])
s = set()
q1 = deque()
q1.append(house)
k1 = 0
s1 = 0
s.add(house)
while len(q1) > 0:
k1 = k1 + 1
if k1 > k + 1:
return s1
n = len(q1)
for i in range(0, n):
t = q1.popleft()
s1 = s1 + t.data
for i1 in d[t]:
if i1 != None and i1 not in s:
s.add(i1)
q1.append(i1)
return s1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NONE ASSIGN VAR NONE WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def dfs(self, root, home, k, d, f):
if d > k:
return False, 0
FP = False
if root.data == home:
f = True
FP = True
if f:
self.ans += root.data
lf, rf = False, False
ld = 0
rd = 0
if root.left:
lf, ld = self.dfs(root.left, home, k, d + 1 if f else d, f)
if lf:
if ld <= k:
self.ans += root.data
if root.right:
self.dfs(root.right, home, k, ld + 1, lf)
if not lf and root.right:
rf, rd = self.dfs(root.right, home, k, d + 1 if f else d, f)
if rf:
if rd <= k:
self.ans += root.data
if root.left:
self.dfs(root.left, home, k, rd + 1, rf)
return (lf or rf or FP, ld + 1 if lf else rd + 1 if rf else d + 1 if FP else d)
def ladoos(self, root, home, k):
self.ans = 0
self.dfs(root, home, k, 0, False)
return self.ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
parent_child = {}
ghar = None
q = deque([root])
while q:
node = q.popleft()
if node.left:
q.append(node.left)
parent_child[node.left] = node
if node.right:
q.append(node.right)
parent_child[node.right] = node
if node.data == home:
ghar = node
max_laddu = 0
q = deque([(ghar, 0)])
visited = set()
while q:
node, dist = q.popleft()
if dist > k:
break
if node.left and node.left not in visited:
q.append((node.left, dist + 1))
if node.right and node.right not in visited:
q.append((node.right, dist + 1))
if node in parent_child and parent_child[node] not in visited:
q.append((parent_child[node], dist + 1))
max_laddu += node.data
visited.add(node)
return max_laddu | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
def fun1(root, k):
if not root or k < 0:
return 0
return root.data + fun1(root.left, k - 1) + fun1(root.right, k - 1)
def fun(root, home, k):
if not root:
return -1, 0
if root.data == home:
return k - 1, fun1(root, k)
else:
left, s1 = fun(root.left, home, k)
right, s2 = fun(root.right, home, k)
if left >= 0:
return left - 1, fun1(root.right, left - 1) + root.data + s1
if right >= 0:
return right - 1, fun1(root.left, right - 1) + root.data + s2
return -1, s1 + s2
ans = fun(root, home, k)
return ans[1] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
pMap = {}
self.fillParentMap(pMap, None, root)
targetNode = self.searchNode(root, home)
return self.bfsTraversal(set(), pMap, targetNode, k)
def fillParentMap(self, pMap, parent, root):
if root is None:
return
pMap[root] = parent
self.fillParentMap(pMap, root, root.left)
self.fillParentMap(pMap, root, root.right)
def searchNode(self, root, target):
if root is None or root.data == target:
return root
left = self.searchNode(root.left, target)
if left is not None:
return left
return self.searchNode(root.right, target)
def bfsTraversal(self, vis, pMap, root, k):
sum = 0
q = []
q.append(root)
vis.add(root)
while len(q) > 0 and k >= 0:
size = len(q)
while size > 0:
curr = q.pop(0)
par = pMap.get(curr)
left = curr.left
right = curr.right
sum += curr.data
if par is not None and par not in vis:
vis.add(par)
q.append(par)
if left is not None and left not in vis:
vis.add(left)
q.append(left)
if right is not None and right not in vis:
vis.add(right)
q.append(right)
size -= 1
k -= 1
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NONE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def __init__(self):
self.ans = 0
def ladoos(self, root, home, k):
self.ans = 0
self.solve(root, home, k)
return self.ans
def solve(self, root, home, k):
if root is None:
return -1
if root.data == home:
self.add(root, k)
return k - 1
rem = self.solve(root.right, home, k)
if rem >= 0:
self.ans += root.data
self.add(root.left, rem - 1)
return rem - 1
rem = self.solve(root.left, home, k)
if rem >= 0:
self.ans += root.data
self.add(root.right, rem - 1)
return rem - 1
return -1
def add(self, n, dist):
if n is None or dist < 0:
return
self.ans += n.data
self.add(n.left, dist - 1)
self.add(n.right, dist - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def helper2(self, root, k):
if not root or k < 0:
return 0
if k == 0:
return root.data
l = self.helper2(root.left, k - 1)
r = self.helper2(root.right, k - 1)
return l + r + root.data
def helper(self, root, home, k):
if not root:
return 0, -1
if root.data == home:
return self.helper2(root, k), k - 1
l, kl = self.helper(root.left, home, k)
if l:
if kl >= 0:
return self.helper2(root.right, kl - 1) + l + root.data, kl - 1
else:
return l, -1
r, kr = self.helper(root.right, home, k)
if r:
if kr >= 0:
return self.helper2(root.left, kr - 1) + r + root.data, kr - 1
else:
return r, -1
return 0, -1
def ladoos(self, root, home, k):
res, _ = self.helper(root, home, k)
return res | CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER RETURN NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def findParent(self, root, mp, vis):
q = []
q.append(root)
while q:
curr = q.pop(0)
vis[curr] = 0
if curr.left:
mp[curr.left] = curr
q.append(curr.left)
if curr.right:
mp[curr.right] = curr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def findTarget(self, root, home):
qu = [root]
while qu:
curr = qu.pop(0)
if curr.data == home:
return curr
if curr.left != None:
qu.append(curr.left)
if curr.right != None:
qu.append(curr.right)
return None
def markParent(self, root, mp):
qu = [root]
while qu:
curr = qu.pop(0)
if curr.left != None:
mp[curr.left] = curr
qu.append(curr.left)
if curr.right != None:
mp[curr.right] = curr
qu.append(curr.right)
def ladoos(self, root, home, k):
ans = 0
parent = {}
target = self.findTarget(root, home)
self.markParent(root, parent)
vis = {target: True}
qu = [target]
level = 0
while len(qu) > 0:
n = len(qu)
if level > k:
break
else:
level += 1
for i in range(n):
curr = qu.pop(0)
ans += curr.data
if curr.left != None and curr.left not in vis:
qu.append(curr.left)
vis[curr.left] = True
if curr.right != None and curr.right not in vis:
qu.append(curr.right)
vis[curr.right] = True
if curr in parent and parent[curr] not in vis:
qu.append(parent[curr])
vis[parent[curr]] = True
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def add_subtree(self, n, dist):
if n is None or dist < 0:
return 0
return (
n.data
+ self.add_subtree(n.left, dist - 1)
+ self.add_subtree(n.right, dist - 1)
)
def traverse(self, n, target, k):
if n is None:
return 0, -1
if n.data == target:
return self.add_subtree(n, k), k - 1
Sum, dist = self.traverse(n.left, target, k)
if Sum > 0:
if dist == -1:
return Sum, dist
return Sum + n.data + self.add_subtree(n.right, dist - 1), dist - 1
Sum, dist = self.traverse(n.right, target, k)
if Sum > 0:
if dist == -1:
return Sum, dist
return Sum + n.data + self.add_subtree(n.left, dist - 1), dist - 1
return 0, -1
def ladoos(self, root, home, k):
Sum, dist = self.traverse(root, home, k)
return Sum | CLASS_DEF FUNC_DEF IF VAR NONE VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
def _precompute(nd, par):
nonlocal start
nd.parent = par
if nd.left:
_precompute(nd.left, nd)
if nd.right:
_precompute(nd.right, nd)
if nd.data == home:
start = nd
def _solve(nd, dis, prev):
if dis < 0:
return 0
ans = nd.data
for nex in (nd.parent, nd.left, nd.right):
if nex is prev or nex is None:
continue
ans += _solve(nex, dis - 1, nd)
return ans
start = None
_precompute(root, None)
ans = _solve(start, k, None)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR NONE VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NONE RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
graph = {}
self.preOrder(root, graph, None)
vis = {}
for i in graph:
vis[i] = 0
temp = graph[home]
vis[home] = 1
ans = 0
while len(temp) > 0 and k > 0:
k -= 1
for i in range(len(temp)):
tempo = temp.pop(0)
if tempo != None and vis[tempo] == 0:
vis[tempo] = 1
ans += tempo
temp += graph[tempo]
return ans + home
def preOrder(self, root, graph, parent):
if root == None:
return None
elif root.data not in graph:
graph[root.data] = [
parent,
self.preOrder(root.left, graph, root.data),
self.preOrder(root.right, graph, root.data),
]
return root.data | CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR NONE ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
ans = 0
q = []
q.append(root)
need = None
m = {}
while q:
s = len(q)
for i in range(s):
temp = q.pop(0)
if temp.data == home:
need = temp
if temp.left:
q.append(temp.left)
m[temp.left] = temp
if temp.right:
q.append(temp.right)
m[temp.right] = temp
mm = {}
q = []
q.append(need)
dis = 0
while q:
s = len(q)
for i in range(s):
temp = q.pop(0)
mm[temp] = True
ans += temp.data
if temp.left and not mm.get(temp.left):
q.append(temp.left)
if temp.right and not mm.get(temp.right):
q.append(temp.right)
if m.get(temp) and not mm.get(m[temp]):
q.append(m[temp])
dis += 1
if dis > k:
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR DICT WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
start = None
def par_asgn(node, par):
nonlocal start
node.parent = par
if node.left:
par_asgn(node.left, node)
if node.right:
par_asgn(node.right, node)
if node.data == home:
start = node
par_asgn(root, None)
def solve(node, dis, prev):
if dis < 0:
return 0
ans = node.data
for next_node in (node.parent, node.left, node.right):
if not next_node or next_node == prev:
continue
ans += solve(next_node, dis - 1, node)
return ans
return solve(start, k, None) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NONE FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR NONE |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
def findforSubtree(root, dis):
if root is None or dis < 0:
return 0
if dis == 0:
return root.data
return (
root.data
+ findforSubtree(root.left, dis - 1)
+ findforSubtree(root.right, dis - 1)
)
def solve(root, home, k, ans):
if root == None:
return -1
if root.data == home:
ans[0] += findforSubtree(root, k)
return 0
leftDistance = solve(root.left, home, k, ans)
rightDistance = solve(root.right, home, k, ans)
if rightDistance != -1:
if rightDistance + 1 <= k:
ans[0] += root.data + findforSubtree(
root.left, k - rightDistance - 2
)
return rightDistance + 1
if leftDistance != -1:
if leftDistance + 1 <= k:
ans[0] += root.data + findforSubtree(
root.right, k - leftDistance - 2
)
return leftDistance + 1
return -1
ans = [0]
solve(root, home, k, ans)
return ans[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
ans = 0
def sum_node(n, k):
nonlocal ans
if not n or k < 0:
return 0
ans += n.data
sum_node(n.left, k - 1)
sum_node(n.right, k - 1)
def walk(n):
nonlocal ans, k
if not n:
return False, 0
if n.data == home:
sum_node(n, k)
return True, k - 1
flag, left = walk(n.left)
if flag and left >= 0:
ans += n.data
sum_node(n.right, left - 1)
return flag, left - 1
flag, right = walk(n.right)
if flag and right >= 0:
ans += n.data
sum_node(n.left, right - 1)
return flag, right - 1
return False, 0
walk(root)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
dict = {}
vis = {}
def rec(root, par):
if root == None:
return
dict[root] = par
if root.data == home:
ho[0] = root
rec(root.left, root)
rec(root.right, root)
ho = [root]
rec(root, None)
queue = [(ho[0], 0)]
sum = 0
while queue:
curr = queue.pop(0)
vis[curr[0]] = 1
sum += curr[0].data
if curr[1] == k:
continue
if curr[0].left and curr[0].left not in vis:
queue.append((curr[0].left, curr[1] + 1))
if curr[0].right and curr[0].right not in vis:
queue.append((curr[0].right, curr[1] + 1))
if curr[0] in dict and dict[curr[0]] and dict[curr[0]] not in vis:
queue.append((dict[curr[0]], curr[1] + 1))
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR NONE ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def helper(self, node, home, k, reach):
if not node:
return 0
if node.data == home:
reach = k + 1
lreach = self.helper(node.left, home, k, reach - 1)
if lreach > reach and lreach > 1:
rreach = self.helper(node.right, home, k, lreach - 1)
else:
rreach = self.helper(node.right, home, k, reach - 1)
if rreach > lreach and rreach > 1:
lreach = self.helper(node.left, home, k, rreach - 1)
localReach = max(max(lreach, rreach), reach)
if localReach > 0:
self.total += node.data
localReach -= 1
return localReach
def ladoos(self, root, home, k):
self.total = 0
self.helper(root, home, k, 0)
return self.total | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, h, k):
g = {}
home = None
v = set()
def graph(node):
nonlocal home
if not node:
return
if node.data == h:
home = node
j = node.left
k = node.right
if node not in g:
g[node] = []
if j:
if j not in g:
g[j] = []
g[j].append(node)
g[node].append(j)
if k:
if k not in g:
g[k] = []
g[k].append(node)
g[node].append(k)
graph(node.left)
graph(node.right)
graph(root)
q = [(home, 0)]
i = 0
res = 0
while i < len(q):
p, r = q[i]
if r > k:
break
i += 1
res += p.data
v.add(p)
for j in g[p]:
if j in v:
continue
q.append((j, r + 1))
return res
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR LIST IF VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def ladoos(self, root, home, k):
if root is None:
return 0
if not root.left and not root.right:
if root.data == home:
return root.data
return 0
temp, q, pos, len1, node = {}, deque(), 0, 1, None
q.append(root)
while pos < len1:
if q[pos].data == home:
node = q[pos]
break
if q[pos].left:
q.append(q[pos].left)
temp[q[pos].left], len1 = q[pos], len1 + 1
if q[pos].right:
q.append(q[pos].right)
temp[q[pos].right], len1 = q[pos], len1 + 1
pos += 1
q, sum1, temp1 = deque(), 0, set()
q.append([node, 0])
while q:
node, k1 = q.popleft()
sum1 += node.data
temp1.add(node)
if node.left and k1 < k and node.left not in temp1:
q.append([node.left, k1 + 1])
if node.right and k1 < k and node.right not in temp1:
q.append([node.right, k1 + 1])
if node in temp and k1 < k and temp[node] not in temp1:
q.append([temp[node], k1 + 1])
return sum1 | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR DICT FUNC_CALL VAR NUMBER NUMBER NONE EXPR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Chhota Bheem wants to eat the maximum number of ladoos in Dholakpur on Independence Day. The houses in Dholakpur are arranged in the form of a binary tree and have ladoos the same as their house number. Chhota Bheem is standing at his home initially.
Find the maximum ladoos he can eat if he can go to houses within a maximum distance k from his house. The number of ladoos at his home should also be included in the sum.
Note: Every house has distinct ladoos in it.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 9, K = 1
Output:
22
Explanation:
Initially Bheem at 9, so sum = 9
In 2nd move he went to 5, sum=9+5=14
In 3rd move he went to 7, sum=14+7=21
In 4th move he went to 1, sum=21+1=22
So within K distance bheem can get 22 ladoos.
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
home = 40, K = 2
Output:
113
Explanation:
Initially Bheem at 40, so sum = 40
In 2nd move he went to 19, sum=40+19=59
In 3rd move he went to 4, sum=59+4=63
In 4th move he went to 50, sum=63+50=113
So within K distance bheem can get 113 ladoos.
Your Task:
You don't need to read input or print anything. Complete the function ladoos() which takes the root of the tree, home, and K as input parameters and returns the maximum number of ladoos he can eat.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(1)
Constraints:
1 ≤ N, Home ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def findParent(self, root, mp, vis):
q = []
q.append(root)
while q:
curr = q.pop(0)
vis[curr] = 0
if curr.left:
mp[curr.left] = curr
q.append(curr.left)
if curr.right:
mp[curr.right] = curr
q.append(curr.right)
def ladoos(self, root, home, k):
mp = {}
vis = {}
self.findParent(root, mp, vis)
q = []
for node, parent in mp.items():
if node.data == home or parent.data == home:
if node.data == home:
q.append([node, 0])
vis[node] = 1
break
if parent.data == home:
q.append([parent, 0])
vis[parent] = 1
break
sum = q[0][0].data
while q:
size = len(q)
lvl = q[0][1]
if lvl == k:
break
lvl += 1
for i in range(size):
curr, curr_lvl = q.pop(0)
if curr.left and vis[curr.left] == 0:
vis[curr.left] = 1
sum += curr.left.data
q.append([curr.left, lvl])
if curr.right and vis[curr.right] == 0:
vis[curr.right] = 1
sum += curr.right.data
q.append([curr.right, lvl])
if mp.get(curr) and vis.get(mp[curr]) == 0:
vis[mp[curr]] = 1
sum += mp[curr].data
q.append([mp[curr], lvl])
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR |
MoEngage has given you an array A consisting of N positive integers.
You should perform Q queries of the following two types:
1 i X : Set A_{i} to X.
2 L R : Find the minimum non-negative integer K such that the sequence [|A_{L} - K|, |A_{L+1} - K|, \ldots, |A_{R-1} - K|, |A_{R} - K|] is sorted in non-decreasing order. Print -1 if no such K exists.
For each query of type 2, output the minimum non-negative integer K satisfying the constraint, or -1 if there isn't any.
------ Input Format ------
- The first line contains two integers N, Q.
- The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- Then, the i^{th} of the next Q lines contains three integers describing the queries 1 i X or 2 L R.
------ Output Format ------
For each query of type 2, output the minimum integer K satisfying the constraint, or -1 if there isn't any in a new line.
------ Constraints ------
$1 ≤ N, Q ≤ 2\cdot 10^{5}$
$0 ≤ A_{i}, X ≤ 10^{9}$
$1 ≤ L ≤ R ≤ N$
----- Sample Input 1 ------
5 7
5 3 2 6 0
2 1 5
1 2 7
2 1 5
1 4 2
2 1 5
1 2 5
2 1 2
----- Sample Output 1 ------
4
-1
5
0
----- explanation 1 ------
- In the first query, selecting $K=4$ makes the sequence $[|A_{1} - K|, |A_{2} - K|, |A_{3} - K|, |A_{4} - K|, |A_{5} - K|] = [1, 1, 2, 2, 4]$. This sequence is sorted in non-decreasing order. Also, there exists no value of $K$ less than $4$ satisfying this.
- In the second query, change $A_{2} = 7$. After the second query, the array $A$ becomes $[5, 7, 2, 6, 0]$.
- In the third query, there exists no value of $K$ satisfying the constraints. | from sys import stdin, stdout
input = stdin.readline
def merge(p1, p2):
if p1[-1] == -1:
return p2
if p2[2] == -1:
return p1
if p1[-1] == p2[2]:
interval = 0, float("inf")
elif p1[-1] < p2[2]:
interval = 0, (p1[-1] + p2[2]) // 2
else:
interval = (p1[-1] + p2[2]) // 2 + (p1[-1] + p2[2]) % 2, float("inf")
return max(p1[0], p2[0], interval[0]), min(p1[1], p2[1], interval[1]), p1[2], p2[-1]
def build(ST, pos, l, r):
if l == r:
ST[pos] = 0, float("inf"), arr[l], arr[l]
return ST[pos]
mid = (l + r) // 2
ST[pos] = merge(build(ST, 2 * pos + 1, l, mid), build(ST, 2 * pos + 2, mid + 1, r))
return ST[pos]
def update(ST, pos, l, r, ind, val):
if l == r:
assert l == ind
arr[l] = val
ST[pos] = 0, float("inf"), arr[l], arr[l]
return
mid = (l + r) // 2
if ind <= mid:
update(ST, 2 * pos + 1, l, mid, ind, val)
else:
update(ST, 2 * pos + 2, mid + 1, r, ind, val)
ST[pos] = merge(ST[2 * pos + 1], ST[2 * pos + 2])
def query(ST, pos, l, r, ql, qr):
if ql <= l and r <= qr:
return ST[pos]
if r < ql or qr < l:
return 0, float("inf"), -1, -1
mid = (l + r) // 2
return merge(
query(ST, 2 * pos + 1, l, mid, ql, qr),
query(ST, 2 * pos + 2, mid + 1, r, ql, qr),
)
t = 1
for _ in range(t):
n, m = map(int, input().split())
arr = [int(x) for x in input().split()]
ST = [(float("inf"), 0) for i in range(4 * n)]
build(ST, 0, 0, n - 1)
for M in range(m):
a, b, c = map(int, input().split())
if a == 1:
b -= 1
update(ST, 0, 0, n - 1, b, c)
else:
b -= 1
c -= 1
l, r, x, y = query(ST, 0, 0, n - 1, b, c)
if l > r:
l = -1
print(l) | ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
MoEngage has given you an array A consisting of N positive integers.
You should perform Q queries of the following two types:
1 i X : Set A_{i} to X.
2 L R : Find the minimum non-negative integer K such that the sequence [|A_{L} - K|, |A_{L+1} - K|, \ldots, |A_{R-1} - K|, |A_{R} - K|] is sorted in non-decreasing order. Print -1 if no such K exists.
For each query of type 2, output the minimum non-negative integer K satisfying the constraint, or -1 if there isn't any.
------ Input Format ------
- The first line contains two integers N, Q.
- The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
- Then, the i^{th} of the next Q lines contains three integers describing the queries 1 i X or 2 L R.
------ Output Format ------
For each query of type 2, output the minimum integer K satisfying the constraint, or -1 if there isn't any in a new line.
------ Constraints ------
$1 ≤ N, Q ≤ 2\cdot 10^{5}$
$0 ≤ A_{i}, X ≤ 10^{9}$
$1 ≤ L ≤ R ≤ N$
----- Sample Input 1 ------
5 7
5 3 2 6 0
2 1 5
1 2 7
2 1 5
1 4 2
2 1 5
1 2 5
2 1 2
----- Sample Output 1 ------
4
-1
5
0
----- explanation 1 ------
- In the first query, selecting $K=4$ makes the sequence $[|A_{1} - K|, |A_{2} - K|, |A_{3} - K|, |A_{4} - K|, |A_{5} - K|] = [1, 1, 2, 2, 4]$. This sequence is sorted in non-decreasing order. Also, there exists no value of $K$ less than $4$ satisfying this.
- In the second query, change $A_{2} = 7$. After the second query, the array $A$ becomes $[5, 7, 2, 6, 0]$.
- In the third query, there exists no value of $K$ satisfying the constraints. | from sys import stdin, stdout
input = stdin.readline
def merge(p1, p2):
if p1[-1] == -1:
return p2
if p2[2] == -1:
return p1
if p1[-1] == p2[2]:
interval = 0, float("inf")
elif p1[-1] < p2[2]:
interval = 0, (p1[-1] + p2[2]) // 2
else:
interval = (p1[-1] + p2[2]) // 2 + (p1[-1] + p2[2]) % 2, float("inf")
return max(p1[0], p2[0], interval[0]), min(p1[1], p2[1], interval[1]), p1[2], p2[-1]
class SegmentTree:
def __init__(self, data, default=(0, float("inf"), -1, -1), func=merge):
data = [default] + data
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size : _size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
stop += 1
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def __repr__(self):
return "SegmentTree({0})".format(self.data)
t = 1
for _ in range(t):
n, m = map(int, input().split())
arr = [int(x) for x in input().split()]
ST = SegmentTree([(0, float("inf"), arr[l], arr[l]) for l in range(n)])
for M in range(m):
a, b, c = map(int, input().split())
if a == 1:
ST.__setitem__(b, (0, float("inf"), c, c))
else:
l, r, x, y = ST.query(b, c)
if l > r:
l = -1
print(l) | ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER CLASS_DEF FUNC_DEF NUMBER FUNC_CALL VAR STRING NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR STRING VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def maptoparent(self, root):
if root is None:
return
if not root.left and not root.right:
return
if root.left:
self.parent[root.left] = root
self.maptoparent(root.left)
if root.right:
self.parent[root.right] = root
self.maptoparent(root.right)
def find(self, root, target):
if root is None:
return
if self.node:
return
if root.data == target:
self.node = root
self.find(root.left, target)
self.find(root.right, target)
def computesum(self, root, k):
if k < 0 or root is None:
return
self.sum += root.data
self.visited.add(root)
if root.left not in self.visited:
self.computesum(root.left, k - 1)
if root.right not in self.visited:
self.computesum(root.right, k - 1)
if root in self.parent:
parent = self.parent[root]
else:
return
if parent not in self.visited:
self.computesum(self.parent[root], k - 1)
def sum_at_distK(self, root, target, k):
self.parent = {}
self.sum = 0
self.node = None
self.maptoparent(root)
self.find(root, target)
self.visited = set()
self.computesum(self.node, k)
return self.sum | CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR RETURN IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NONE RETURN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def find_parent_and_target(self, root, parent, target):
global d
global node
if not root:
return
if target == root.data:
node = root
d[root] = [parent, False]
self.find_parent_and_target(root.left, root, target)
self.find_parent_and_target(root.right, root, target)
def find_ans(self, root, k):
global d
if not root:
return 0
if d[root][1]:
return 0
if k == 0:
return root.data
d[root][1] = True
return (
root.data
+ self.find_ans(root.left, k - 1)
+ self.find_ans(root.right, k - 1)
+ self.find_ans(d[root][0], k - 1)
)
def sum_at_distK(self, root, target, k):
global d
d = {}
global node
node = None
self.find_parent_and_target(root, None, target)
return self.find_ans(node, k) | CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR NONE VAR RETURN FUNC_CALL VAR VAR VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, home, k):
def findforSubtree(root, dis):
if root is None or dis < 0:
return 0
if dis == 0:
return root.data
return (
root.data
+ findforSubtree(root.left, dis - 1)
+ findforSubtree(root.right, dis - 1)
)
def solve(root, home, k, ans):
if root == None:
return -1
if root.data == home:
ans[0] += findforSubtree(root, k)
return 0
leftDistance = solve(root.left, home, k, ans)
rightDistance = solve(root.right, home, k, ans)
if rightDistance != -1:
if rightDistance + 1 <= k:
ans[0] += root.data + findforSubtree(
root.left, k - rightDistance - 2
)
return rightDistance + 1
if leftDistance != -1:
if leftDistance + 1 <= k:
ans[0] += root.data + findforSubtree(
root.right, k - leftDistance - 2
)
return leftDistance + 1
return -1
ans = [0]
solve(root, home, k, ans)
return ans[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
parents = dict()
queue = deque()
queue.append(root)
x = None
while queue:
curr = queue.popleft()
if curr.data == target:
x = curr
if curr.left:
parents[curr.left] = curr
queue.append(curr.left)
if curr.right:
parents[curr.right] = curr
queue.append(curr.right)
d = dict()
queue.clear()
queue.append(x)
d[x] = 1
ans = 0
i = 0
while queue:
if i > k:
break
i += 1
n = len(queue)
for j in range(n):
root = queue.popleft()
ans += root.data
if root.left != None and root.left not in d:
queue.append(root.left)
d[root.left] = 1
if root.right != None and root.right not in d:
queue.append(root.right)
d[root.right] = 1
if root in parents and parents[root] not in d:
queue.append(parents[root])
d[parents[root]] = 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def maptoparent(self, root, target):
q = [root]
pos, len1 = 0, 1
while pos < len1:
top = q[pos]
if top.data == target:
self.node = top
if top.left:
q.append(top.left)
len1 += 1
self.parent[top.left] = top
if top.right:
q.append(top.right)
len1 += 1
self.parent[top.right] = top
pos += 1
def computesum(self, k):
self.sum = self.node.data
q = [[self.node, k]]
pos, len1 = 0, 1
visited = {self.node}
while pos < len1:
node, k = q[pos]
print("k =", k, node.data)
if k >= 0:
if node.left and node.left not in visited:
self.sum += node.left.data
q.append([node.left, k - 1])
len1 += 1
if node.right and node.right not in visited:
self.sum += node.right.data
q.append([node.right, k - 1])
len1 += 1
if node in self.parent:
parent = self.parent[node]
if parent not in self.visited:
self.sum += node.parent.data
q.append([parent, k - 1])
len1 += 1
pos += 1
return self.sum
def computesum(self, root, k):
if k < 0 or root is None:
return
self.sum += root.data
self.visited.add(root)
if root.left not in self.visited:
self.computesum(root.left, k - 1)
if root.right not in self.visited:
self.computesum(root.right, k - 1)
if root in self.parent:
parent = self.parent[root]
else:
return
if parent not in self.visited:
self.computesum(self.parent[root], k - 1)
def sum_at_distK(self, root, target, k):
self.parent = {}
self.sum = 0
self.node = None
self.visited = set()
self.maptoparent(root, target)
self.computesum(self.node, k)
return self.sum | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER VAR NONE RETURN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
q = []
q.append(root)
parents = {}
targetNode = Node(-1)
while q:
node = q.pop(0)
if node.data == target:
targetNode = node
if node.left and node.left not in parents:
parents[node.left] = node
q.append(node.left)
if node.right and node.right not in parents:
parents[node.right] = node
q.append(node.right)
visited = {}
visited[targetNode] = 1
q1 = []
q1.append(targetNode)
lvl = 0
Sum = 0
while q1:
if lvl > k:
break
sze = len(q1)
for i in range(sze):
node = q1.pop(0)
Sum += node.data
if node.left is not None and node.left not in visited:
q1.append(node.left)
visited[node.left] = 1
if node.right is not None and node.right not in visited:
q1.append(node.right)
visited[node.right] = 1
if node in parents:
if parents[node] is not None and parents[node] not in visited:
visited[parents[node]] = 1
q1.append(parents[node])
lvl += 1
return Sum | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
if root is None:
return 0
def findSum(root, k):
if root is None or k < 0:
return 0
return root.data + findSum(root.left, k - 1) + findSum(root.right, k - 1)
def helper(root, target, k):
if root is None:
return 0, -1
if root.data == target:
sm = findSum(root, k)
return sm, k - 1
sm, dist = helper(root.left, target, k)
if sm > 0:
if dist == -1:
return sm, dist
sm = sm + findSum(root.right, dist - 1) + root.data
return sm, dist - 1
sm, dist = helper(root.right, target, k)
if sm > 0:
if dist == -1:
return sm, dist
sm = sm + findSum(root.left, dist - 1) + root.data
return sm, dist - 1
return 0, -1
sm, dist = helper(root, target, k)
return sm | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER FUNC_DEF IF VAR NONE VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def create(self, root, parent, tar, target):
if root == None:
return None
if root.data == target:
tar[0] = root
l = self.create(root.left, parent, tar, target)
r = self.create(root.right, parent, tar, target)
if l != None:
parent[l] = root
if r != None:
parent[r] = root
return root
def check_nabor(self, visited, root, parent, count, ans, k):
if root == None or visited[root]:
return
visited[root] = True
if count <= k:
ans[0] += root.data
self.check_nabor(visited, root.left, parent, count + 1, ans, k)
self.check_nabor(visited, root.right, parent, count + 1, ans, k)
self.check_nabor(visited, parent[root], parent, count + 1, ans, k)
def sum_at_distK(self, root, target, k):
parent, visited = {}, {}
parent[root] = None
ans, tar = [0], [None]
dic = self.create(root, parent, tar, target)
for i in parent:
visited[i] = False
self.check_nabor(visited, tar[0], parent, 0, ans, k)
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR VAR RETURN ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR DICT DICT ASSIGN VAR VAR NONE ASSIGN VAR VAR LIST NUMBER LIST NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR NUMBER |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
parent = dict()
start = [None]
self.fillUpParent(root, parent, start, target)
totalSum = 0
visited = set()
queue = [(start[0], 0)]
while queue:
node, currentK = queue.pop(0)
if currentK > k:
continue
totalSum += node.data
visited.add(node)
if node in parent and parent[node] not in visited:
queue.append((parent[node], currentK + 1))
if node.left and node.left not in visited:
queue.append((node.left, currentK + 1))
if node.right and node.right not in visited:
queue.append((node.right, currentK + 1))
return totalSum
def fillUpParent(self, root, parent, start, target):
if not root:
return
if root.data == target and not start[0]:
start[0] = root
if root.left:
parent[root.left] = root
if root.right:
parent[root.right] = root
self.fillUpParent(root.left, parent, start, target)
self.fillUpParent(root.right, parent, start, target) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
parentmap = {}
targetNode = self.markPar(root, parentmap, target)
cursum = self.sumUtil(root, parentmap, targetNode)
return cursum
def sumUtil(self, root, parentmap, targetNode):
cursum = 0
que = deque()
que.append(targetNode)
visit = dict()
visit[targetNode] = 1
dis = 0
while que:
if dis > k:
break
dis += 1
for i in range(len(que)):
node = que.popleft()
cursum += node.data
if node.left and node.left not in visit:
que.append(node.left)
visit[node.left] = 1
if node.right and node.right not in visit:
que.append(node.right)
visit[node.right] = 1
if node in parentmap and parentmap[node] not in visit:
que.append(parentmap[node])
visit[parentmap[node]] = 1
return cursum
def markPar(self, root, parentmap, target):
if root is None:
return
res = None
q = deque()
q.append(root)
while q:
node = q.popleft()
if node.data == target:
res = node
if node.left:
q.append(node.left)
parentmap[node.left] = node
if node.right:
q.append(node.right)
parentmap[node.right] = node
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def solve2(self, p, k):
if not p:
return
if k > 0:
self.ans += p.data
self.solve2(p.left, k - 1)
self.solve2(p.right, k - 1)
def solve(self, p, target, k):
if not p:
return 0, -1
if p.data == target:
self.ans += p.data
self.solve2(p.left, k)
self.solve2(p.right, k)
return 1, k
left = self.solve(p.left, target, k)
if left[0] == 1:
if left[1] > 0:
self.ans += p.data
self.solve2(p.right, left[1] - 1)
return 1, left[1] - 1
right = self.solve(p.right, target, k)
if right[0] == 1:
if right[1] > 0:
self.ans += p.data
self.solve2(p.left, right[1] - 1)
return 1, right[1] - 1
return 0, -1
def sum_at_distK(self, root, target, k):
self.ans = 0
self.solve(root, target, k)
return self.ans | CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Solution:
def sum_at_distK(self, root, target, k):
path = self.findPath(root, target, "")
return self.findSum(root, k, path, target)
def findPath(self, root, target, path):
if root is None:
return ""
if root.data == target:
return path
left = self.findPath(root.left, target, path + "l")
right = self.findPath(root.right, target, path + "r")
return left + right
def findSum(self, root, k, path, target):
if root is None or not k:
return 0
k += root.data == target
if not path:
k -= 1
summa = root.data
summa += self.findSum(root.left, k, path, target)
summa += self.findSum(root.right, k, path, target)
return summa
n = len(path)
if path[0] == "l":
if n > k:
return self.findSum(root.left, k, path[1:], target)
if n == k:
return self.findSum(root.left, k, path[1:], target) + root.data
left = self.findSum(root.left, k, path[1:], target)
right = self.findSum(root.right, k - n, "", target)
return left + right + root.data
if n > k:
return self.findSum(root.right, k, path[1:], target)
if n == k:
return self.findSum(root.right, k, path[1:], target) + root.data
left = self.findSum(root.right, k, path[1:], target)
right = self.findSum(root.left, k - n, "", target)
return left + right + root.data | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN STRING IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR RETURN BIN_OP BIN_OP VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially.
Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum.
Example 1:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 9, K = 1
Output:
22
Explanation:
Nodes within distance 1 from 9 are 9, 5, 7, 1
Example 2:
Input:
1
/ \
2 9
/ / \
4 5 7
/ \ / \
8 19 20 11
/ / \
30 40 50
target = 40, K = 2
Output:
113
Explanation:
Nodes within distance 2 from 40 are 40, 19, 50, 4
Your Task:
You don't need to read input or print anything. Complete the function sum_at_distK() which takes the root of the tree, target, and K as input parameter and returns the sum of all nodes within a max distance of k from the target
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N, Node Value ≤ 10^{5}
1 ≤ K ≤ 20 | class Node_:
def __init__(self, node):
node.head = None
class Solution:
def sum_at_distK(self, root, target, k):
sum_ = [0]
Node_(root)
root.head = None
if root.left:
Node_(root.left)
root.left.head = root
if root.right:
Node_(root.right)
root.right.head = root
self.sum_at_distKUtil(root, target, k, sum_)
return sum_[0]
def sum_at_distKUtil(self, root, target, k, sum_):
if target == root.data:
self.front(root, k, sum_)
self.back(root.head, k - 1, sum_)
return
if root.left:
Node_(root.left)
root.left.head = root
self.sum_at_distKUtil(root.left, target, k, sum_)
if root.right:
Node_(root.right)
root.right.head = root
self.sum_at_distKUtil(root.right, target, k, sum_)
def back(self, root, k, sum_):
if k >= 0 and root and root.data:
sum_[0] += root.data
root.data = 0
self.back(root.head, k - 1, sum_)
self.front(root.left, k - 1, sum_)
self.front(root.right, k - 1, sum_)
def front(self, root, k, sum_):
if k >= 0 and root and root.data:
sum_[0] += root.data
root.data = 0
self.front(root.left, k - 1, sum_)
self.front(root.right, k - 1, sum_) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
a = None
while root is not None:
if root.data == x:
return root.data
elif root.data > x:
root = root.left
else:
a = root
root = root.right
if a is None:
return -1
else:
return a.data | CLASS_DEF FUNC_DEF ASSIGN VAR NONE WHILE VAR NONE IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, key):
floor = -1
while root:
if root.data == key:
floor = root.data
return floor
if key > root.data:
floor = root.data
root = root.right
else:
root = root.left
return floor | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
c = -1
while root:
if x > root.data:
c = root.data
root = root.right
elif x < root.data:
root = root.left
else:
return x
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
ans = -1
curr = root
while curr:
while curr and curr.data < x:
ans = curr.data
curr = curr.right
if curr and curr.data == x:
return x
if curr:
curr = curr.left
def recur(root, x):
if root is None:
return -1
if root.data == x:
return x
if root.data > x:
return recur(root.left, x)
else:
return max(root.data, recur(root.right, x))
return recur(root, x) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
m = -1
while root:
if root.data <= x:
m = max(m, root.data)
root = root.right
else:
root = root.left
return m | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
ans = -1
curr = root
while curr:
while curr and curr.data < x:
ans = curr.data
curr = curr.right
if curr and curr.data == x:
return x
if curr:
curr = curr.left
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
curr = root
prevmin = -1
while curr is not None:
if curr.data == x:
return curr.data
if curr.data < x:
prevmin = curr.data
curr = curr.right
else:
curr = curr.left
return prevmin | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
if not root:
return -1
prev = None
p = root
while p:
if p.data == x:
return x
elif p.data < x:
prev = p
p = p.right
else:
p = p.left
return -1 if not prev else prev.data | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
s = [root]
res = [root.data]
while s:
node = s.pop(0)
if node.left:
s.append(node.left)
res.append(node.left.data)
if node.right:
s.append(node.right)
res.append(node.right.data)
res.sort()
ans = []
for i in res:
if i <= x:
ans.append(i)
if len(ans) == 0:
return -1
return max(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
def findPreSuc(root, pre, suc, key):
if root == None:
return
findPreSuc(root.left, pre, suc, key)
if root.data <= key:
pre[0] = root.data
if root.data > key:
if suc[0] == None or root.data < suc[0]:
suc[0] = root.data
findPreSuc(root.right, pre, suc, key)
pre = [-1]
suc = [-1]
findPreSuc(root, pre, suc, x)
return pre[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR IF VAR NUMBER NONE VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def answer(self, root, ans):
if root.left:
self.answer(root.left, ans)
ans.append(root.data)
if root.right:
self.answer(root.right, ans)
def floor(self, root, x):
if not root:
return -1
ans = []
self.answer(root, ans)
num = -1
for i in ans:
if i > x:
return num
if i == x:
return i
num = i
if num > x:
return -1
return num | CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
queue = [root]
ans = []
while queue:
node = queue.pop(0)
ans.append(node.data)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
ans.sort()
num = ans[0]
if num > x:
return -1
for i in ans:
if i > x:
return num
elif i == x:
return i
num = i
return num | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def __init__(self):
self.prev = -1
def floor(self, root, x):
if root is None:
return self.prev
if root.data == x:
return root.data
if root.data > x:
return self.floor(root.left, x)
if root.data < x:
self.prev = root.data
return self.floor(root.right, x) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def helper(self, root, inp, ans):
if not root:
return ans
if root.data == inp:
ans = inp
return ans
if root.data > inp:
return self.helper(root.left, inp, ans)
if root.data < inp:
ans = root.data
return self.helper(root.right, inp, ans)
def floor(self, root, x):
if not root:
return -1
ans = -1
return self.helper(root, x, ans) | CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
if root is None:
return -1
if root.data == x:
return x
if root.data > x:
return self.floor(root.left, x)
val = self.floor(root.right, x)
if val <= x and val != -1:
return val
else:
return root.data | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
if not root:
return -1
s = []
p = root
while p:
if p.data == x:
return x
elif p.data < x:
s.append(p)
p = p.right
else:
p = p.left
return -1 if len(s) == 0 else s[-1].data | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR IF VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def find(self, root, tar, ans):
if root == None:
return ans
if root.data == tar:
ans = tar
return ans
if root.data < tar:
ans = root.data
return self.find(root.right, tar, ans)
elif root.data > tar:
return self.find(root.left, tar, ans)
def floor(self, root, x):
ans = -1
if root == None:
return -1
return self.find(root, x, ans) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
def inorder(root):
nonlocal ans, x
if not root:
return
inorder(root.left)
if root.data <= x:
if ans == -1:
ans = root.data
elif ans < root.data:
ans = root.data
inorder(root.right)
ans = -1
inorder(root)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
mini = -999
def floor(self, root, x):
if root == None:
if self.mini == -999:
return -1
return self.mini
if root.data < x and root.data > self.mini:
self.mini = root.data
if root.data == x:
return root.data
if root.data < x:
return self.floor(root.right, x)
elif root.data > x:
return self.floor(root.left, x) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE IF VAR NUMBER RETURN NUMBER RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
floor = -1
def helper(node):
nonlocal floor
if not node:
return
if node.data <= x:
floor = node.data
return helper(node.right)
else:
return helper(node.left)
helper(root)
return floor | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
li = self.inorder(root, [])
if x in li:
return x
else:
li.append(x)
li.sort()
x = li.index(x)
if x == 0:
return -1
else:
return li[x - 1]
def inorder(self, node, a):
if node is None:
return
self.inorder(node.left, a)
a.append(node.data)
self.inorder(node.right, a)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP 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 RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def floor(self, root, x):
return self.floorHelper(root, x, -1)
def floorHelper(self, root, x, prev):
if not root:
return prev
if root.data == x:
return x
elif root.data > x:
if root.left:
return self.floorHelper(root.left, x, prev)
else:
return prev
elif root.right:
return self.floorHelper(root.right, x, root.data)
else:
return root.data | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def __init__(self):
self.res = -1
def inorder(self, temp, key):
if temp is None:
return None
self.inorder(temp.right, key)
if key == temp.data or temp.data < key:
if self.res == -1:
self.res = temp.data
self.inorder(temp.left, key)
def floor(self, root, x):
self.inorder(root, x)
return self.res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
You are given a BST(Binary Search Tree) with n number of nodes and value x. your task is to find the greatest value node of the BST which is smaller than or equal to x.
Note: when x is smaller than the smallest node of BST then returns -1.
Example:
Input:
n = 7 2
\
81
/ \
42 87
\ \
66 90
/
45
x = 87
Output:
87
Explanation:
87 is present in tree so floor will be 87.
Example 2:
Input:
n = 4 6
\
8
/ \
7 9
x = 11
Output:
9
Your Task:
You don't need to read input or print anything. Complete the function floor() which takes the integer n and BST and integer x returns the floor value.
Constraint:
1 <= n <= 10^{5}
Expected Time Complexity: O(n)
Expected Space Complexity: O(1) | class Solution:
def __init__(self):
self.ans = -1
def floor(self, root, x):
if not root:
return -1
if root.data == x:
self.ans = x
return x
if root.data < x:
self.ans = root.data
if root.data > x:
self.floor(root.left, x)
else:
self.floor(root.right, x)
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.