description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int, input().split()))
d1 = {}
d2 = {}
for i in range(n):
b = bin(a[i])[2:]
lb = len(b)
if lb not in d1:
d1[lb] = a[i]
else:
d1[lb] = max(d1[lb], a[i])
for i in range(n):
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR ... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def bc(x, y):
binx = bin(x).replace("0b", "")
biny = bin(y).replace("0b", "")
return int(binx + biny, 2) - int(biny + binx, 2)
def large(A, n):
A.sort()
m = -99999999999
for i in range(n - 1):
mx = abs(bc(A[i], A[n - 1]))
if mx > m:
m = mx
print(m)
def small(A... | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CAL... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def BinaryConcatenation(X, Y):
binX = str(bin(X).replace("0b", ""))
binY = str(bin(Y).replace("0b", ""))
binXplusY = binX + binY
binYplusX = binY + binX
XplusY = int(binXplusY, 2)
YplusX = int(binYplusX, 2)
return abs(XplusY - YplusX)
t = int(input())
for _ in range(t):
n = int(input()... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_C... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def binfun(x, y):
binx = str(bin(x))
binx = binx[2:]
biny = str(bin(y))
biny = biny[2:]
binxpy = binx + biny
binypx = biny + binx
return abs(int(binxpy, 2) - int(binypx, 2))
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = 0
t = max(a)
... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR A... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
b.append(0)
while i > 0:
b[-1] += 1
i = i >> 1
ans = -1
if n >= 500:
j = a.index(max(a))
for i in range(n):
x = a[i] * 2 ** b[... | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUN... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
count = 0
b = []
for i in range(n):
b.append(bin(a[i]).replace("0b", ""))
c = b[0]
d = b[0]
for i in range(n):
p = b[i] + c
q = c + b[i]
p = int(p, 2)
q = int(q, 2)
... | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NU... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | minimumValue, maximumValue = -1000000000, 1000000000
def solve(arr, N):
maxx, minn = [minimumValue] * 32, [maximumValue] * 32
for i in range(N):
binaryLength = 0
for j in range(31, -1, -1):
bit = arr[i] & 1 << j
if bit > 0 or binaryLength > 0:
binaryLeng... | ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP LIST VAR NUMBER BIN_OP LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VA... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
max_bit = [0] * 32
min_bit = [10**9] * 32
for i in range(n):
b = len(bin(a[i])) - 2
max_bit[b] = max(max_bit[b], a[i])
min_bit[b] = min(min_bit[b], a[i])
ans = -1
for i in range(1, 32... | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | INF = float("inf")
def f(x, y):
return abs(int(bin(x)[2:] + bin(y)[2:], 2) - int(bin(y)[2:] + bin(x)[2:], 2))
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
l = len(bin(i)[2:])
d[l] = d.get(l, [INF, -INF])
if i < d[l]... | ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def binConcatenate(x, y):
x = bin(x)[2:]
y = bin(y)[2:]
a = int(x + y, 2)
b = int(y + x, 2)
return abs(a - b)
t = int(input())
while t:
n = int(input())
arr = list(map(int, input().split()))
ans = []
temp = max(arr)
if n < 500:
for i in range(n):
for j in ra... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL ... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def bincon(a, b):
X = bin(a).replace("0b", "")
Y = bin(b).replace("0b", "")
XpY = int(X + Y, 2)
YpX = int(Y + X, 2)
return max(XpY - YpX, YpX - XpY)
for _ in range(int(input())):
n = int(input())
llist = list(map(int, input().split()))
ans = 0
if n < 1000:
for u in range(n)... | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | t = int(input())
while t > 0:
t -= 1
n = int(input())
arr = list(map(int, input().strip().split(" ")))
dp = []
for i in range(31):
k = []
dp.append(k)
for num in arr:
for j in range(30, -1, -1):
temp = 1 << j
if temp & num:
dp[j].ap... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBE... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | for tcase in range(int(input())):
n = int(input())
a = sorted(map(int, input().split()))
b = [a[i].bit_length() for i in range(n)]
c = [a[i] for i in range(n) if i == 0 or i == n - 1 or b[i - 1] < b[i + 1]]
d = [c[i].bit_length() for i in range(len(c))]
print(
max(
[
... | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NU... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | def binary(X, Y):
str_x = bin(X)[2:]
str_y = bin(Y)[2:]
XplusY = str_x + str_y
YplusX = str_y + str_x
Xnew = int("0b" + XplusY, 2)
Ynew = int("0b" + YplusX, 2)
return abs(Xnew - Ynew)
t = int(input())
for i in range(0, t):
n = int(input())
list1 = list(map(int, input().split()))
... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | import sys
for _ in range(int(input())):
n = int(input())
arr = [int(j) for j in input().split()]
pw = [1] * 31
for i in range(1, 31):
pw[i] = 2 * pw[i - 1]
mx = [0] * 32
mn = [sys.maxsize] * 32
for j in arr:
l = len(bin(j)[2:])
mx[l] = max(mx[l], j)
mn[l] = ... | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN... |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary repres... | t = int(input())
def func(a, b):
bin_a = bin(a).replace("0b", "")
bin_b = bin(b).replace("0b", "")
bin_X = bin_a + bin_b
bin_y = bin_b + bin_a
p = int(bin_X, 2)
q = int(bin_y, 2)
return max(p - q, q - p)
while t != 0:
n = int(input())
a = list(map(int, input().split()))
ans =... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP... |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today the... | import sys
input = sys.stdin.readline
def main():
n, q = map(int, input().split())
S = input().strip()
cum = [0]
for i, s in enumerate(S):
if (s == "+") ^ (i % 2 == 1):
cum.append(cum[-1] + 1)
else:
cum.append(cum[-1] - 1)
for _ in range(q):
l, r = ... | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FU... |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today the... | import sys
from _collections import defaultdict
input = sys.stdin.readline
T = int(input())
def bilower(a, x):
if len(a) == 0:
return -1
mi = 0
ma = len(a) - 1
if a[0] > x:
return -1
if a[ma] <= x:
return ma
while ma - mi > 1:
mid = (ma + mi) // 2
if a... | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN... |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today the... | from sys import stdin, stdout
raw_input = input
xrange = range
raw_input = lambda: stdin.readline().rstrip()
input = lambda: int(raw_input())
I = lambda: map(int, raw_input().split())
def f(l, r):
global pprefix, prefix, p
xl = sign(g(l))
if xl == 0:
return l
xr = sign(g(r))
if xr == 0:
... | ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASS... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
def fun(grid):
n = len(grid)
m = len(grid[0])
queue = []
visited = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
if grid[i][j]... | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMB... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
ct = 0
res = -1
q = Queue()
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] > 0:
ct += 1
if gr... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, gp):
d = dict()
leng = 0
for i in range(len(gp)):
for j in range(len(gp[0])):
d[i, j] = float("inf")
for i in range(len(gp)):
for j in range(len(gp[0])):
if gp[i][j] == 2:
... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSI... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
q = []
m, n = len(grid), len(grid[0])
count_oranges = 0
grid1 = grid
for i in range(m):
for j in range(n):
if grid1[i][j] == 2:
q.append((i, j))
if grid1[i][j] == ... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR ... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
row = len(grid)
col = len(grid[0])
vis = [[(0) for j in range(col)] for i in range(row)]
q = []
time = 0
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]
for i in range(row):
for j in range(col):
... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
arr = grid
que = []
l = len(arr[0])
for i in range(len(arr)):
for j in range(l):
if arr[i][j] == 2:
que.append([i, j, 0])
else:
continue
t = 0
... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, a):
q = []
n = len(a)
m = len(a[0])
for i in range(n):
for j in range(m):
if a[i][j] == 2:
q.append((i, j, 0))
ans = 0
while len(q) > 0:
l = len(q)
for i ... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN V... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, a):
ans = 0
q = []
n = len(a)
m = len(a[0])
for i in range(n):
for j in range(m):
if a[i][j] == 2:
q.append((i, j, 0))
while len(q) != 0:
row, col, c = q.pop(0)
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR ... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
rows = len(grid)
cols = len(grid[0])
queue = []
fresh_count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 2:
queue.append((i, j))
elif grid[i][j... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUM... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
queue = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 2:
queue.append((i, j, 0))
time_taken = 0
while queue:
i, j, time_taken = queue.pop(0)
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VA... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
n = len(grid)
m = len(grid[0])
s = []
cntfresh = 0
vis = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
if grid[i][j] == 2:
s.append([i,... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER I... |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh ... | class Solution:
def orangesRotting(self, grid):
def do(i, j, temp):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]):
return
if grid[i][j] == 1:
grid[i][j] += 1
temp.append([i, j])
rotten = []
temp = []
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NU... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
if not root:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if left == None and right == None:
return None... | CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, p, q):
def answer(root):
if root == None or root.data == p or root.data == q:
return root
lt = answer(root.left)
rt = answer(root.right)
if rt == None:
return lt
if lt == None:
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def findNode(root, l, x):
if root is None:
return False
l.append(root)
if root.data == x:
return True
if findNode(root.left, l, x) or findNode(root.right, l, x):
ret... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSI... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def commonAncestor(root, k1, k2):
if root != None:
l1, l2, ld = commonAncestor(root.left, k1, k2)
r1, r2, rd = commonAncestor(root.right, k1, k2)
Key1_found = True if root.data == k1 else l1 or r1
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NONE VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR RETURN NUMBER NUMBER NONE ASSIGN VAR VAR... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
in_left_tree = self.lca(root.left, n1, n2)
in_right_tree = self.lca(root.right, n1, n2)
if in_left_tree and in_right_tree:
... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if left and right:
return root
eli... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def solve(root):
if not root:
return root, False
if root.data == n1 or root.data == n2:
return root, True
if root:
lr, ls = solve(root.left)
rr, rs = solve(root.... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR NUMBER IF VAR VAR VAR VAR RETURN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR NUMBER IF VAR RETURN VAR NUMBER IF VAR RETURN VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
ans = []
def dfs(root, n, ans=[]):
ans.append(root)
if root.data == n:
return True
if root.left:
if dfs(root.left, n, ans):
return True
if root.right:
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF LIST EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR ... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | def roottoleaf(root, node, path):
if root == None:
return False
path.append(root)
if root.data == node:
return True
if roottoleaf(root.left, node, path) or roottoleaf(root.right, node, path):
return True
path.pop(-1)
return False
class Solution:
def lca(self, root,... | FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VA... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | def findLCA(root, n1, n2, ans, curr):
if root == None:
return
if root.data == n1:
ans[0] = curr[0]
if root.data == n2:
ans[1] = curr[0]
if ans[0] != "N" and ans[1] != "N":
return
if root.left:
findLCA(root.left, n1, n2, ans, [curr[0] + "0"])
if root.right:... | FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER STRING CLASS_DEF FUNC_DEF ASS... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Node:
def __init__(self, value):
self.left = None
self.data = value
self.right = None
class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(roo... | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def util(self, root, n1, n2):
if root == None:
return None
if root.data == n1 or root.data == n2:
return root
l = self.util(root.left, n1, n2)
r = self.util(root.right, n1, n2)
if l != None and r != None:
return root
... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def findlca(root, n1, n2, ans):
if root is None:
return 0
leftval = findlca(root.left, n1, n2, ans)
if leftval == 2:
return leftval
rightval = findlca(root.right, n1, n2, ans)
... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST NONE EXPR FUNC_... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Node:
def __init__(self, value):
self.left = None
self.data = value
self.right = None
class Solution:
def lca(self, root, n1, n2):
if root == None:
return Node(-1)
if root.data == n1 or root.data == n2:
return root
c1 = self.lca(r... | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETUR... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
if not root or root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if not left:
return right
elif not right:
return left
... | CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR IF VAR RETURN VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def findNode(self, root, n1, n2):
if not root:
return None
if root.data == n1 or root.data == n2:
return root
leftAns = self.findNode(root.left, n1, n2)
rightAns = self.findNode(root.right, n1, n2)
if leftAns != None and rightAns != No... | CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def LCAPath(root, path, tar):
if root == None:
return
if root.data == tar:
self.ans.append(path + [root.data])
LCAPath(root.left, path + [root.data], tar)
LCAPath(root.right, path +... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR ... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | def find_ancestors(root, n, original_ancestors):
ancestors = original_ancestors.copy()
if root == None:
return None
elif root.data == n:
ancestors.append(root)
return ancestors
ancestors.append(root)
leftfound = find_ancestors(root.left, n, ancestors)
if leftfound != None... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NONE RETURN NONE IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR ... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
def pathLCA(root, path, tar):
if root == None:
return
if root.data == tar:
self.ans.append(path + [root.data])
return
pathLCA(root.left, path + [root.data], tar)
pat... | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSI... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def rec_sol(self, root, n1, n2, ans):
if root == None:
return -1
left_ans = self.rec_sol(root.left, n1, n2, ans)
right_ans = self.rec_sol(root.right, n1, n2, ans)
if root.data == n1 or root.data == n2:
if left_ans == 1 or right_ans == 1:
... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMB... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | def find_lca_using_recursion(value_1, value_2, node):
if node is None:
return False, False, None
left = find_lca_using_recursion(value_1, value_2, node.left)
right = find_lca_using_recursion(value_1, value_2, node.right)
found_1 = value_1 == node.data or left[0] or right[0]
found_2 = value_2... | FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NONE RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
if root is None:
return 0
def fun(root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
l = fun(root.left, n1, n2)
r = f... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
data = [n1, n2]
Node1 = Node(0)
ans = [Node1]
def F(root):
if root == None:
return False
left = F(root.left)
right = F(root.right)
if left == True and right == True:
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUN... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def store(self, root, data=[]):
if root is None:
return
self.dct[root.data] = data + [root.data]
data = data + [root.data]
self.store(root.left, data)
self.store(root.right, data)
def lca(self, root, n1, n2):
self.dct = {}
sel... | CLASS_DEF FUNC_DEF LIST IF VAR NONE RETURN ASSIGN VAR VAR BIN_OP VAR LIST VAR ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def findpath(self, root, path, x):
if root is None:
return False
path.append(root)
if root.data == x:
return True
if self.findpath(root.left, path, x) or self.findpath(root.right, path, x):
return True
path.pop()
re... | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR LIST LIST IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NONE ASSIGN VAR NUMBER WHILE ... |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of... | class Solution:
def lca(self, root, n1, n2):
v = [False, False]
lca = self.findLCA(root, n1, n2, v)
if v[0] and v[1] or v[0] and self.find(lca, n2) or v[1] and self.find(lca, n1):
return lca
return None
def find(self, root, k):
if root is None:
r... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not len(level):
return None
rootVal = level[0]
root = Node(rootVal)
ind = 0
while ino[ind] != rootVal:
ind += 1
leftIno = ino[:ind]
rightIno = ino[ind + 1 :]
leftLevel = []
rightLevel = []
for val in level[1:]:
if val in leftI... | FUNC_DEF IF FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if len(ino) == 1:
return Node(ino[0])
mp = {ino[i]: i for i in range(len(ino))}
for i in range(len(level)):
val = level[i]
if val in mp:
root = Node(val)
root_idx = mp[val]
break
if root_idx > 0:
root.left = b... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VA... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def find(level, ino):
lev = []
for i in range(len(level)):
for j in range(len(ino)):
if ino[j] == level[i]:
lev.append(level[i])
return lev
def cons(level, ino, n):
dict = {}
for i in range(n):
dict[ino[i]] = i
rootIndex = dict[level[0]]
root = N... | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN V... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
def construct(start, end):
if start > end:
return
index = l[ino[start]]
ans = start
for i in range(start, end + 1):
if l[ino[i]] < index:
index = l[ino[i]]
ans = i
root = Node(ino[ans])
... | FUNC_DEF FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
root = Node(level[0])
m = ino.index(root.data)
leftin = ino[:m]
rightin = ino[m + 1 :]
leftlevel = level[1:]
rightlevel = level
rightlevel.remove(root.data)
for i in leftin:
try:
rightlevel.remove(i)
except:
continue
... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_C... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if level is None and ino is None:
return None
elif level is None and ino is not None or level is not None and ino is None:
return None
else:
root = Node(level[0])
ino_root_idx = ino.index(level[0])
ino_left, ino_right = [], []
for i ... | FUNC_DEF IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE VAR NONE VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buil(level, ino):
if ino:
for i in range(len(level)):
if level[i] in ino:
node = Node(level[i])
index = ino.index(level[i])
break
node.left = buil(level, ino[0:index])
node.right = buil(level, ino[index + 1 : len(ino)])
... | FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VA... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if ino:
for i in range(len(level)):
if level[i] in ino:
curr = Node(level[i])
x = ino.index(level[i])
break
curr.left = buildTree(level, ino[:x])
curr.right = buildTree(level, ino[x + 1 :])
return ... | FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | class Node:
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def buildTree(level, ino):
if ino:
for i in range(0, len(level)):
if level[i] in ino:
node = Node(level[i])
io_index = ino.index(level[i])
... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR V... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def f(level: list, inorder: list, indices: dict):
if len(level) == 0:
return None
cur = Node(level[0])
idx = indices[level[0]]
left_level, right_level = [], []
for i in range(1, len(level)):
if indices[level[i]] < idx:
left_level.append(level[i])
else:
... | FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
curr = 0
n = len(ino)
root = Node(level[0])
curr += 1
q = [root]
while curr != n:
node = q.pop(0)
node.left = Node(level[curr])
curr += 1
q.append(node.left)
if curr != n:
node.right = Node(level[curr])
cu... | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def construct_tree(levelorder, inorder):
if len(levelorder) == 0:
return None
cur_node = Node(levelorder[0])
for i in range(0, len(inorder)):
if cur_node.data == inorder[i]:
cur_index = i
left_inorder = inorder[0:cur_index]
right_inorder = inorder[cur_index + 1 :]
lef... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VA... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
start = 0
end = len(ino) - 1
if start > end:
return -1
Hash = search(ino)
root = tobinarytree(level, ino, start, end, Hash)
return root
def tobinarytree(level, ino, start, end, Hash):
if start > end:
return None
data = ino[(start + end) // 2]
... | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE RETU... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def func(level, ino, mp):
if len(level) < 1:
return
ele = level[0]
root = Node(ele)
if len(level) == 1:
return root
root_ix = mp[ele]
l_level = []
r_level = []
for i in range(len(level)):
if mp[level[i]] < root_ix:
l_level.append(level[i])
elif... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | class makenode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def buildTree1(inorder, levelorder, iStart, iEnd, n):
if n <= 0:
return None
root = makenode(levelorder[0])
index = -1
for i in range(iStart, iEnd + 1):
if levelor... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR AS... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, inorder):
if not level or not inorder:
return None
root = Node(level[0])
root_inorder_index = inorder.index(level[0])
left_inorder = set(inorder[:root_inorder_index])
right_inorder = set(inorder[root_inorder_index + 1 :])
level_order_left = [i for i in level if i in ... | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not level:
return None
if not ino:
return None
curr = level[0]
root = Node(curr)
i = ino.index(curr)
li = set(ino[:i])
lef = []
for ele in level:
if ele in li:
lef.append(ele)
li = set(ino[i + 1 :])
ler = []
fo... | FUNC_DEF IF VAR RETURN NONE IF VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CA... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not ino or not level:
return None
root = Node(level[0])
index = ino.index(root.data)
leftTree = ino[:index]
rightTree = ino[index + 1 :]
levelLeft, levelRight = [], []
for value in level[1:]:
if value in leftTree:
levelLeft.append(val... | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR V... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def construct(in_order, lvl_order):
if len(in_order) == 0 or len(lvl_order) == 0:
return None
res = Node(lvl_order.pop(0))
q = [res]
in_ = [in_order]
while len(q) > 0:
root = q.pop(0)
in_order = in_.pop(0)
if root.data in in_order:
root_index = in_order.in... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def helper(level, ino, in_left, in_right, mapping):
if in_left > in_right:
return None
root = Node(level[0])
in_idx = mapping[level[0]]
left_level = []
right_level = []
for ele in level:
idx = mapping[ele]
if idx >= in_left and idx < in_idx:
left_level.append(... | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def f(l, r, inorder: list, indices: dict):
if l > r:
return None
min_idx = l
for i in range(l + 1, r + 1):
if indices[inorder[i]] < indices[inorder[min_idx]]:
min_idx = i
cur = Node(inorder[min_idx])
cur.left = f(l, min_idx - 1, inorder, indices)
cur.right = f(min_idx... | FUNC_DEF VAR VAR IF VAR VAR RETURN NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF AS... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
LevelOrder = level
InOrder = ino
LevelOrderMap = {}
for i in LevelOrder:
LevelOrderMap[i] = 0
def MakeTree(LevelOrder, InOrder, LevelOrderMap):
if LevelOrder == []:
return None
root = Node(LevelOrder[0])
for i in range(0, len(In... | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR LIST RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR V... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class ConstructTree:
def __init__(self):
self.dict_ = dict()
def ConstructMap(self, level):
for i in range(0, len(level)):
key = level[i]
self.dict_[... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def ff(l, i):
if not l:
return
y = i.index(l[0])
r = Node(l[0])
c = []
d = []
for j in l:
if j in i[:y]:
c.append(j)
if j in i[y + 1 :]:
d.append(j)
r.left = ff(c, i[:y])
r.right = ff(d, i[y + 1 :])
return r
def buildTree(l, i):
r... | FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RET... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def find(ino, lev):
for i in range(len(ino)):
if ino[i] == lev:
return i
def buildTree(levo, ino):
if not ino:
return
count = find(ino, levo[0])
node = Node(levo.pop(0))
new_level = []
for i in range(len(levo) - 1, -1, -1):
if levo[i] in ino[:count]:
... | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CA... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not level or not ino:
return
new_node = Node(level[0])
idx = ino.index(level[0])
left_ino = ino[:idx]
left_level = [x for x in level if x in left_ino]
new_node.left = buildTree(left_level, left_ino)
right_ino = ino[idx + 1 :]
right_level = [x for x i... | FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def f(ino, i, j, d):
if i > j:
return
mn = i, ino[i]
for k in range(i, j + 1):
if d[ino[k]] < d[mn[1]]:
mn = k, ino[k]
root = Node(mn[1])
root.left = f(ino, i, mn[0] - 1, d)
root.right = f(ino, mn[0] + 1, j, d)
return root
def buildTree(level, ino):
d = dict... | FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR FU... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
def build(ino):
if not ino:
return None
else:
for ele in level:
if ele in ino:
idx = ino.index(ele)
root = Node(ele)
break
root.left = build(ino[0:idx])
... | FUNC_DEF FUNC_DEF IF VAR RETURN NONE FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
root = None
n = len(level)
if n <= 0:
return None
nodeList = []
for i in level:
nodeList.append(Node(i))
for i in range(n // 2):
node = nodeList[i]
next_left_node = 2 * i + 1
if next_left_node < n:
node.left = nodeLis... | FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASS... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(levelorder, inorder):
n = len(inorder)
map = dict()
for i in range(n):
map[inorder[i]] = i + 1
def tempfunc(li, ri, levelorder):
nonlocal map, inorder
if li > ri:
return None
if li == ri:
return Node(inorder[li - 1])
k = map[... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR N... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
return constructTree_levelorder_inorder(level, ino)
def constructTree_levelorder_inorder(levelorder, inorder):
if len(inorder) == 0:
return None
rootData = -1
rootIndex = -1
for i in levelorder:
if i in inorder:
rootData = i
rootIn... | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_O... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not level:
return
root = Node(level[0])
index = 1
queue = [root]
while index < len(level):
newLevel = []
for node in queue:
counter = 2
left = True
while counter > 0 and index < len(level):
newN... | FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def f(level, inorder):
if len(inorder) == 0:
return None
cur = Node(level[0])
idx = inorder.index(level[0])
left_level, right_level = [], []
for i in range(1, len(level)):
if inorder.index(level[i]) < idx:
left_level.append(level[i])
else:
right_level.... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
return solve(level, 0)
def solve(level1, i):
root = None
if i < len(level1):
root = Node(level1[i])
root.left = solve(level1, 2 * i + 1)
root.right = solve(level1, 2 * i + 2)
return root | FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if len(ino) == 0:
return None
node = Node(level[0])
hasset = set()
left_level = []
right_level = []
i = 0
idx = ino.index(level[0])
while i < idx:
hasset.add(ino[i])
i += 1
for ele in level[1:]:
if ele in hasset:
... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if len(level) != len(ino):
return None
d = dict()
for i in range(len(ino)):
d[ino[i]] = i
def vfx(ino, isi, iei, level, d):
if isi > iei:
return None
root = Node(level[0])
if len(level) == 1:
return root
... | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def constructTree(level, inorder):
if len(level) == 0:
return
nodeVal = level[0]
node = Node(nodeVal)
for index, val in enumerate(inorder):
if val == nodeVal:
break
left_inorder = inorder[:index]
right_inorder = inorder[index + 1 :]
left_level = list()
right_l... | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR AS... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def find(ino, level, m, n):
arr = []
for i in range(len(level)):
if level[i] in ino[m:n]:
arr.append(level[i])
return arr
def buildTree(level, ino):
if len(level) == 0:
return None
root = Node(level[0])
root.left = None
root.right = None
ele_to_search = leve... | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
if not level and not ino:
return None
else:
root = level[0]
r = Node(root)
idx = ino.index(root)
il = ino[0:idx]
ir = ino[idx + 1 :]
ll = []
lr = []
for i in range(1, len(level)):
if level[i] in il:
... | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR A... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def preorder_traversal(root, res):
if root == None:
return
res.append(root.data)
preorder_traversal(root.left, res)
preorder_traversal(root.right, res)
def aux_func(level, ino, ino_idx_dict, il, ir):
if not level:
return None
val = level[0]
root = Node(val)
idx = ino_id... | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
n = len(ino)
if n == 0:
return None
data = level[0]
index = -1
for i in range(n):
if ino[i] == data:
index = i
break
if index == -1:
return None
root = Node(data)
li = ino[:index]
ri = ino[index + 1 :]
ll ... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def buildTree(level, ino):
n = len(level)
return buildTreeUtil(level, ino, 0, n - 1, n)
def buildTreeUtil(level, ino, start, end, n):
if n <= 0 or start > end:
return None
root = level[0]
root_index = get_index(ino, start, end, root)
in_left_set = set(ino[start:root_index])
left_le... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR E... |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level... | def build(ino, start, end, d):
if start > end:
return None
if start == end:
return Node(ino[start])
idx = start
for i in range(start + 1, end + 1):
if d[ino[i]] < d[ino[idx]]:
idx = i
root = Node(ino[idx])
root.left = build(ino, start, idx - 1, d)
root.rig... | FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBE... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.