description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | x = str(input())
dic = {(0, 0): 1}
a = b = c = 0
ans = 0
for i in x:
if i == "A":
a += 1
elif i == "B":
b += 1
else:
c += 1
if (a - b, a - c) not in dic:
dic[a - b, a - c] = 1
else:
dic[a - b, a - c] += 1
for i in dic.values():
ans += i * (i - 1) // 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | t = list(input())
count = 0
k = {}
k[0, 0, 0] = 1
a = 0
b = 0
c = 0
for i in range(len(t)):
if t[i] == "A":
a += 1
elif t[i] == "B":
b += 1
elif t[i] == "C":
c += 1
mi = min(a, b, c)
if (a - mi, b - mi, c - mi) in k.keys():
count += k[a - mi, b - mi, c - mi]
k[a - mi, b - mi, c - mi] += 1
else:
k[a - mi, b - mi, c - mi] = 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = list(input())
l = len(s)
a = [0] * (l + 1)
b = [0] * (l + 1)
c = [0] * (l + 1)
d = {}
counta = 0
countb = 0
countc = 0
for i in range(l):
if s[i] == "A":
counta += 1
elif s[i] == "B":
countb += 1
else:
countc += 1
a[i + 1] = counta
b[i + 1] = countb
c[i + 1] = countc
ans = 0
for i in range(l + 1):
d[a[i] - b[i], a[i] - c[i]] = d.get((a[i] - b[i], a[i] - c[i]), 0) + 1
ans += d[a[i] - b[i], a[i] - c[i]] - 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = input()
dic = {(0, 0): 1}
count = 0
ac = 0
bc = 0
cc = 0
for i in range(len(s)):
if s[i] == "A":
ac += 1
elif s[i] == "B":
bc += 1
else:
cc += 1
pair = ac - bc, ac - cc
count += dic.get(pair, 0)
dic[pair] = dic.get(pair, 0) + 1
print(count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = input()
n = len(s)
count_A = [0] * n
count_B = [0] * n
count_C = [0] * n
for i in range(n):
if s[i] == "A":
count_A[i] += 1
elif s[i] == "B":
count_B[i] += 1
else:
count_C[i] += 1
for i in range(1, n):
a, b, c = (
count_A[i] + count_A[i - 1],
count_B[i] + count_B[i - 1],
count_C[i] + count_C[i - 1],
)
x = min(a, b, c)
count_A[i], count_B[i], count_C[i] = a - x, b - x, c - x
counts = {}
counts[0, 0, 0] = 1
for i in range(n):
key = count_A[i], count_B[i], count_C[i]
counts[key] = counts.get(key, 0) + 1
ans = 0
for v in counts.values():
if v:
ans += v * (v - 1) // 2
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = input()
def fun(s):
i = 0
j = 1
c = 0
mp = {}
mp[0, 0] = 1
a = 0
b = 0
c = 0
t = 0
res = 0
for i in s:
if i == "A":
a += 1
elif i == "B":
b += 1
else:
c += 1
t = c - a, c - b
if t not in mp:
res += 0
else:
res += mp[t]
if t not in mp:
mp[t] = 1
else:
mp[t] += 1
return res
print(fun(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | def list_int_input():
return [int(val) for val in input().split()]
st = input()
freq = [0, 0, 0]
mp = {}
mp[0, 0] = 1
ans = 0
for ix in range(len(st)):
cur_ix = 0 if st[ix] == "A" else 1 if st[ix] == "B" else 2
freq[cur_ix] += 1
pair = freq[0] - freq[1], freq[1] - freq[2]
count = mp.get(pair, 0)
ans += count
mp[pair] = mp.get(pair, 0) + 1
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING NUMBER VAR VAR STRING NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | def dostuff(s):
d = {"0,0": 1}
ans = 0
ta = 0
tb = 0
tc = 0
for i in range(len(s)):
t = s[i]
if t == "A":
ta += 1
if t == "B":
tb += 1
if t == "C":
tc += 1
k = str(ta - tb) + "," + str(ta - tc)
if k in d:
ans += d[k]
d[k] += 1
else:
d[k] = 1
print(ans)
s = input()
dostuff(s) | FUNC_DEF ASSIGN VAR DICT STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = "#" + input().strip()
n = len(s)
d = {}
sm = 0
a = 0
b = 0
c = 0
for i in range(n):
if s[i] == "A":
a += 1
if s[i] == "B":
b += 1
if s[i] == "C":
c += 1
sm += d.get((a - b, a - c), 0)
d[a - b, a - c] = d.get((a - b, a - c), 0) + 1
print(sm) | ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | from sys import stdin
input = stdin.readline
def answer():
pa, pb, pc = [0], [0], [0]
for i in range(n):
pa.append(pa[-1] + (s[i] == "A"))
pb.append(pb[-1] + (s[i] == "B"))
pc.append(pc[-1] + (s[i] == "C"))
d = dict()
for i in range(n + 1):
x, y, z = pa[i] - pb[i], pb[i] - pc[i], pc[i] - pa[i]
d[x, y, z] = d.get((x, y, z), 0) + 1
ans = 0
for x in d.values():
ans += x * (x - 1) // 2
return ans
for T in range(1):
s = input().strip()
n = len(s)
print(answer()) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | try:
string = input()
dictk = {(0, 0): 1}
a = 0
b = 0
c = 0
res = 0
for s in string:
if s == "A":
a += 1
elif s == "B":
b += 1
else:
c += 1
t = c - a, c - b
if t in dictk:
res += dictk[t]
dictk[t] += 1
else:
dictk[t] = 1
print(res)
except:
pass | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | z = input()
a, b, c = 0, 0, 0
res = 0
dict = {}
dict[0, 0] = 1
for i in z:
if i == "A":
a += 1
elif i == "B":
b += 1
elif i == "C":
c += 1
k = c - a, c - b
if k in dict:
res += dict[k]
dict[k] = dict.get(k, 0) + 1
print(res) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | import sys
from sys import maxsize
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_list_string():
return list(map(str, sys.stdin.readline().strip().split()))
def get_string():
return sys.stdin.readline().strip()
def get_int():
return int(sys.stdin.readline().strip())
def get_print_int(x):
sys.stdout.write(str(x) + "\n")
def get_print(x):
sys.stdout.write(x + "\n")
def get_print_int_same(x):
sys.stdout.write(str(x) + " ")
def get_print_same(x):
sys.stdout.write(x + " ")
def solve():
s = get_string()
n = len(s)
d = dict()
d[0, 0] = 1
a, b, c = 0, 0, 0
ans = 0
for i in range(n):
if s[i] == "A":
a += 1
elif s[i] == "B":
b += 1
else:
c += 1
other = a - b, a - c
if other in d:
ans += d[other]
d[other] += 1
else:
d[other] = 1
get_print_int(ans)
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = input()
kfreq = {(0, 0): 1}
ai = bi = ci = 0
for ch in s:
if ch == "A":
ai += 1
elif ch == "B":
bi += 1
elif ch == "C":
ci += 1
ki = ai - bi, ai - ci
if ki in kfreq:
kfreq[ki] += 1
else:
kfreq[ki] = 1
ways = 0
for freq in kfreq.values():
kfc2 = freq * (freq - 1) // 2
ways += kfc2
print(ways) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string s whose length is n and array queries of length q where each elements of queries is either of type 1 query or type 2 query which is explained ahead.
There are two types of query.
Query type 1 : ["1",ind,char] "1" denotes this is type 1 query. In this query you have to change the character at index ind in s to character char.(Data type of ind,char is string in input)
Query Type 2: ["2",left,right,k] "2" denotes this is type 2 query. In this query you have to return kth lexographically largest character in the subtring of s (it is the kth largest character in sorted order , not the kth distinct character) starting from index left and ending at index right both left and right are inclusive. (Data type of left,right,k is string in input)
You have to perform each query in the same order as given in queries and return an array res such that res array contains the answer for each type2 query in same order as it appeared in queries.
Note : 0 based indexing is used.
Example 1:
Input:
n=4
s="abab"
q=2
queries={{"1","2","d"},{"2","1","3","1"}}
Output:
{"d"}
Explanation:
First query is of type 1 so after changing character at index 2
to d s becomes abdb . Now Second query is of type 2 in which
the 1st(k=1) lexographically largest character is "d" in substring "bdb"(s[1:3]). So we
returned a array with result of type 2 query {"d"}.
Example 2:
Input:
n=3
s="aaa"
q=3
queries={{"1","1","e"},{"1","2","c"},{"2","1","2","2"}}
Output:
{"c"}
Explanation:
After applying first two queries s becomes aec. Now for
the last query which is a type 2 second largest character
in subtring s starting from index 1 to ending at index 2 is "c".
Your Task:
You don't need to read input or print anything. Your task is to complete the function easyTask() which takes an integer n,string s,an integer q and an array queries which contains queries of type1 and type2 respectively and returns an array res such that res array contains the answer for each type2 query in same order as it appeared in queries.
Expected Time Complexity: O(N+(Q*logN))
Expected Space Complexity: O(N)
Constraints:
1<=n<=5*10^4
1<=q<=10^5
0<=int(left)<=int(right)<=n-1
0<=int(index)<=n-1
1<=int(k)<=right-left+1
s and char contains lowercase english letters
The sum of n over all test cases won't exceed 5*10^4. | class SegmentTree:
def __init__(self, s):
n = len(s)
self.tree = [([0] * 26) for _ in range(4 * n)]
self.buildTree(s, 0, n - 1, 0)
def buildTree(self, s, left, right, index=0):
if left == right:
self.tree[index][ord(s[left]) - ord("a")] += 1
return
mid = (left + right) // 2
self.buildTree(s, left, mid, 2 * index + 1)
self.buildTree(s, mid + 1, right, 2 * index + 2)
for c in range(26):
self.tree[index][c] = (
self.tree[2 * index + 1][c] + self.tree[2 * index + 2][c]
)
def update(self, s, left, right, pos, char, index=0):
if pos < left or pos > right:
return
if left == right:
self.tree[index][ord(s[pos]) - ord("a")] -= 1
s[pos] = char
self.tree[index][ord(s[pos]) - ord("a")] += 1
return
mid = (left + right) // 2
if pos <= mid:
self.update(s, left, mid, pos, char, 2 * index + 1)
else:
self.update(s, mid + 1, right, pos, char, 2 * index + 2)
for c in range(26):
self.tree[index][c] = (
self.tree[2 * index + 1][c] + self.tree[2 * index + 2][c]
)
def query(self, left, right, i, j, index=0):
if right < i or left > j:
return [0] * 26
if i <= left and right <= j:
return self.tree[index]
mid = (left + right) // 2
temp = [0] * 26
res1 = self.query(left, mid, i, j, 2 * index + 1)
res2 = self.query(mid + 1, right, i, j, 2 * index + 2)
for c in range(26):
temp[c] = res1[c] + res2[c]
return temp
class Solution:
def easyTask(self, n, s, q, queries):
s = [el for el in s]
seg = SegmentTree(s)
res = []
for quer in queries:
if quer[0] == "1":
seg.update(s, 0, n - 1, int(quer[1]), quer[2])
else:
char_freq = seg.query(0, n - 1, int(quer[1]), int(quer[2]))
k = int(quer[3])
for c in range(25, -1, -1):
k -= min(k, char_freq[c])
if k == 0:
res.append(chr(ord("a") + c))
break
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF NUMBER IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF NUMBER IF VAR VAR VAR VAR RETURN BIN_OP LIST NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR RETURN VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def dfs(self, node):
if not node:
return 0
sl = sr = float("inf")
if node.left:
sl = self.dfs(node.left)
if node.right:
sr = self.dfs(node.right)
sm = min(sl, sr)
if sm == float("inf"):
return node.data
return sm + node.data
def maxDifferenceBST(self, root, target):
sm1 = 0
cur = root
while cur.data != target:
flag = False
sm1 += cur.data
if cur.left and cur.data > target:
cur = cur.left
flag = True
elif cur.right and cur.data < target:
cur = cur.right
flag = True
if not flag:
return -1
sm2 = self.dfs(cur) - target
return sm1 - sm2 | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
p = root
a = 0
while p.data != target:
a += p.data
if p.data > target and p.left != None:
p = p.left
elif p.data < target and p.right != None:
p = p.right
else:
break
if p.data != target:
return -1
b = 0
c = 1000000000.0
d = [[p.left, 0], [p.right, 0]]
while len(d) > 0:
r = d.pop()
if r[0] != None:
r[1] += r[0].data
if r[0].left == None and r[0].right == None:
b = max(b, r[1])
c = min(c, r[1])
d.append([r[0].left, r[1]])
d.append([r[0].right, r[1]])
if a - c > a - b:
return a - c
else:
return a - b | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NONE VAR NUMBER VAR NUMBER IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
ans = 0
sum1 = 0
def maxDifferenceBST(self, root, target):
temp = root
Solution.sum1 = 0
while temp:
if temp.data == target:
break
if target < temp.data:
Solution.sum1 += temp.data
temp = temp.left
else:
Solution.sum1 += temp.data
temp = temp.right
if not temp:
return -1
Solution.ans = float("-inf")
if not temp.left and not temp.right:
return Solution.sum1
if temp.left:
Solution.fun(temp.left, 0)
if temp.right:
Solution.fun(temp.right, 0)
return Solution.ans
@staticmethod
def fun(curr, sum2):
if not curr:
return
if not curr.left and not curr.right:
sum2 += curr.data
Solution.ans = max(Solution.ans, Solution.sum1 - sum2)
return
Solution.fun(curr.left, sum2 + curr.data)
Solution.fun(curr.right, sum2 + curr.data) | CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | import sys
class Solution:
def path(self, root, target, sum1, lst):
if root is None:
return None
sum1 += root.data
lst.append(sum1)
if root.data == target:
return root
if target > root.data:
return self.path(root.right, target, sum1, lst)
else:
return self.path(root.left, target, sum1, lst)
def leafNode(self, newRoot):
if newRoot.left == None and newRoot.right == None:
return newRoot.data
a = sys.maxsize
if newRoot.left:
a = min(a, self.leafNode(newRoot.left))
if newRoot.right:
a = min(a, self.leafNode(newRoot.right))
return a + newRoot.data
def maxDifferenceBST(self, root, target):
x = 0
lst = []
targetNode = self.path(root, target, x, lst)
x = lst[-1]
if targetNode == None:
return -1
sum2 = self.leafNode(targetNode)
ans = x - sum2
return ans | IMPORT CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def find(self, root, target):
if root == None:
return None
if root.data == target:
return root
if root.data > target:
return self.find(root.left, target)
else:
return self.find(root.right, target)
def top(self, root, nodeval):
if root == None:
return 0
if root.data == nodeval:
return root.data
if root.data > nodeval:
return root.data + self.top(root.left, nodeval)
else:
return root.data + self.top(root.right, nodeval)
def bot(self, node):
if node == None:
return 0
if node.left == None and node.right == None:
return node.data
if node.left == None:
return node.data + self.bot(node.right)
if node.right == None:
return node.data + self.bot(node.left)
return min(node.data + self.bot(node.left), node.data + self.bot(node.right))
def maxDifferenceBST(self, root, target):
node = self.find(root, target)
if node == None:
return -1
else:
sum1 = self.top(root, node.data) - node.data
sum2 = self.bot(node) - node.data
return sum1 - sum2 | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def find(root, target, ans):
if root == None:
return -1, 0
if root.data == target:
return ans, root
if root.data < target:
ans += root.data
return find(root.right, target, ans)
else:
ans += root.data
return find(root.left, target, ans)
ans = 0
ts, root = find(root, target, ans)
if ts == -1:
return -1
def finms(root, ans1):
if root == None:
return 0
if root.left == None and root.right == None:
return root.data
if root.left == None:
return root.data + finms(root.right, ans)
if root.right == None:
return root.data + finms(root.left, ans)
else:
return root.data + min(finms(root.left, ans), finms(root.right, ans))
ans1 = float("inf")
tms = finms(root, ans1)
return ts - (tms - root.data) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def result(root):
if root == None:
return 1000000000.0
if root.left == None and root.right == None:
return root.data
l = result(root.left)
r = result(root.right)
return min(l, r) + root.data
val = [0]
def solve(root, t):
if root == None:
return -1
if root.data == t:
val[0] = min(result(root.left), result(root.right))
return 0
if root.data > t:
ans = solve(root.left, t)
else:
ans = solve(root.right, t)
if ans != -1:
return ans + root.data
else:
return -1
res = solve(root, target)
if val[0] == 1000000000.0:
return res
return res - val[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def solve(node):
if not node:
return 0
if node.left and node.right:
return node.data + min(solve(node.left), solve(node.right))
else:
return node.data + solve(node.left) + solve(node.right)
rtot = 0
temp = root
while temp:
if temp.data == target:
ttol = solve(temp) - target
return rtot - ttol
elif temp.data < target:
rtot += temp.data
temp = temp.right
else:
rtot += temp.data
temp = temp.left
return -1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def __init__(self):
self.sum_list = []
self.sum_root_to_target = 0
self.sum_target_to_leaf = 0
self.target_node = None
def maxDifferenceBST(self, root, target):
self.root_to_targeT_sum(root, target)
if self.sum_root_to_target == -1:
return -1
self.target_to_leaf_sum(self.target_node)
max_value = max(list(self.sum_root_to_target - i for i in self.sum_list))
return max_value
def root_to_targeT_sum(self, root, target):
i_node = root
if i_node is None:
self.sum_root_to_target = -1
elif target == i_node.data:
self.target_node = i_node
elif target < i_node.data:
self.sum_root_to_target += i_node.data
self.root_to_targeT_sum(i_node.left, target)
else:
self.sum_root_to_target += i_node.data
self.root_to_targeT_sum(i_node.right, target)
def target_to_leaf_sum(self, target, value=0):
if target.left is not None or target.right is not None:
sum_lef = 0
sum_rig = 0
if target.left is not None:
sum_lef = self.target_to_leaf_sum(
target.left, value=target.left.data + value
)
if target.right is not None:
sum_rig = self.target_to_leaf_sum(
target.right, value=target.right.data + value
)
else:
self.sum_list.append(value)
return | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF NUMBER IF VAR NONE VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
val = 0
queue = [(root, val)]
def solve(r):
if r.left == None and r.right == None:
return r.data
if r.right and r.left:
return r.data + min(solve(r.right), solve(r.left))
if r.right:
return r.data + solve(r.right)
if r.left:
return r.data + solve(r.left)
while queue:
x = queue.pop(0)
node = x[0]
val = x[1]
if node.data == target:
ans = solve(node) - node.data
return val - ans
if node.left:
queue.append((node.left, val + node.data))
if node.right:
queue.append((node.right, val + node.data))
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def t_to_c(root, cur):
nonlocal y
if root.left == None and root.right == None:
cur += root.data
y = min(cur, y)
return
if root.left:
t_to_c(root.left, cur + root.data)
if root.right:
t_to_c(root.right, cur + root.data)
def root_to_t(root, target):
nonlocal x
if root == None:
return root
if root.data == target:
return root
x += root.data
if root.data < target:
return root_to_t(root.right, target)
else:
return root_to_t(root.left, target)
x, y = 0, float("inf")
tmp = root_to_t(root, target)
if tmp == None:
return -1
t_to_c(tmp, 0)
y -= target
return x - y | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR NONE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def func(root, target, li, sumi):
if root is None:
return False
if root.data == target:
li.append([sumi, root])
return True
left = func(root.left, target, li, sumi + root.data)
right = func(root.right, target, li, sumi + root.data)
return left or right
li = []
func(root, target, li, 0)
if not li:
return -1
total = li[0][0]
rootdata = li[0][1]
def func1(rootdata):
if rootdata.left is None and rootdata.right is None:
return rootdata.data
if rootdata.left is None and rootdata.right:
return rootdata.data + func1(rootdata.right)
if rootdata.left and rootdata.right is None:
return rootdata.data + func1(rootdata.left)
left = func1(rootdata.left)
right = func1(rootdata.right)
return min(left + rootdata.data, right + rootdata.data)
kk = func1(rootdata)
ans = kk - target
return total - ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
l = [0]
def find(root, target):
if root == None:
return None
if root.data == target:
return root
l[0] += root.data
if root.data > target:
return find(root.left, target)
if root.data < target:
return find(root.right, target)
return None
def findMin(root):
if root.left == None and root.right == None:
return root.data
if root.right == None:
return findMin(root.left) + root.data
if root.left == None:
return findMin(root.right) + root.data
return min(findMin(root.right), findMin(root.left)) + root.data
node = find(root, target)
if node == None:
return -1
return l[0] - (findMin(node) - node.data) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN VAR VAR NUMBER VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN NONE FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR IF VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def dfs(node):
res = node.data
minimum = float("inf")
if node.left:
minimum = min(minimum, dfs(node.left))
if node.right:
minimum = min(minimum, dfs(node.right))
if minimum == float("inf"):
return res
else:
return minimum + res
sum_target = 0
while root and root.data != target:
if root.data > target:
sum_target += root.data
root = root.left
elif root.data < target:
sum_target += root.data
root = root.right
if not root:
return -1
return sum_target - dfs(root) + root.data | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
target_node, root_sum = self.find_target_node(root, target, 0)
if not target_node:
return -1
leaf_sum = self.target_to_leaf_sum(target_node, 0)
return root_sum - (leaf_sum - target_node.data)
def target_to_leaf_sum(self, target, sum_):
if not target:
return float("inf")
if not target.left and not target.right:
return sum_ + target.data
return min(
self.target_to_leaf_sum(target.left, sum_ + target.data),
self.target_to_leaf_sum(target.right, sum_ + target.data),
)
def find_target_node(self, node, target, sum_):
if not node:
return node, -1
if node.data == target:
return node, sum_
if target < node.data:
return self.find_target_node(node.left, target, sum_ + node.data)
return self.find_target_node(node.right, target, sum_ + node.data) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN VAR NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
try:
def dfs(root):
if not root:
return float("inf"), float("-inf"), False
if root.left is None and root.right is None:
if root.data == target:
return 0, 0, True
return root.data, root.data, False
min1, max1, found1 = dfs(root.left)
min2, max2, found2 = dfs(root.right)
if found1:
return min1 + root.data, max1, True
if found2:
return min2 + root.data, max2, True
if root.data == target:
if min1 == float("inf") and min2 == float("inf"):
return 0, 0, True
if min1 == float("inf"):
return 0, min2, True
if min2 == float("inf"):
return 0, min1, True
return 0, min(min1, min2), True
return min(min1, min2) + root.data, max(max1, max2) + root.data, False
s1, s2, found = dfs(root)
if not found:
return -1
return s1 - s2
except Exception as e:
print(e)
return 0 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER IF VAR NONE VAR NONE IF VAR VAR RETURN NUMBER NUMBER NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR RETURN BIN_OP VAR VAR VAR NUMBER IF VAR RETURN BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING RETURN NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
global targetNode
targetNode = None
sum1 = self.getRootToTargetSum(root, target)
if not targetNode:
return -1
sum2 = self.targetToRoot(targetNode)
return sum1 - sum2
def getRootToTargetSum(self, root, target):
if not root:
return -(10**5)
if target == root.data:
global targetNode
targetNode = root
return target
if target < root.data:
return root.data + self.getRootToTargetSum(root.left, target)
else:
return root.data + self.getRootToTargetSum(root.right, target)
def targetToRoot(self, target):
if not target:
return 10**5
if not target.left and not target.right:
return target.data
left = target.data + self.targetToRoot(target.left)
right = target.data + self.targetToRoot(target.right)
return min(left, right) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR RETURN BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN BIN_OP NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def minpath(root):
if not root:
return 0
l = minpath(root.left)
r = minpath(root.right)
if l == 0:
l = 99999999999
if r == 0:
r = 99999999999
if l == 99999999999 and r == 99999999999:
l, r = 0, 0
return root.data + min(l, r)
x = [None]
psum = [0]
def find(root):
if not root:
return
psum[0] += root.data
if root.data == target:
x[0] = root
if root.data < target:
find(root.right)
if root.data > target:
find(root.left)
find(root)
if x[0]:
xsum = minpath(x[0]) - x[0].data
psum[0] -= x[0].data
return psum[0] - xsum
else:
return -1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NONE ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR RETURN VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR RETURN NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
x = []
z = []
def helper(root, t):
if not root:
return False
if root.data == t:
z.append(root)
x.append(root.data)
return True
l = helper(root.left, t)
r = helper(root.right, t)
if l or r:
x.append(root.data)
return True
return False
helper(root, target)
if not x:
return -1
def helper2(root):
if not root:
return 999999999
if root.left == None and root.right == None:
return root.data
l = helper2(root.left)
r = helper2(root.right)
return root.data + min(l, r)
a = helper2(z[0])
return sum(x) - a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def mxsm(root):
if not root:
return -1
if root.data == target:
mxsm.targetRef = root
return 0
if target < root.data:
temp = mxsm(root.left)
else:
temp = mxsm(root.right)
return temp + root.data if temp != -1 else -1
def mnsm(root):
if not root:
return float("inf")
if not root.left and not root.right:
return root.data
left = mnsm(root.left)
right = mnsm(root.right)
mn = min(left, right)
return mn + root.data
_a = mxsm(root)
if _a == -1:
return -1
_b = mnsm(mxsm.targetRef) - mxsm.targetRef.data
return _a - _b | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
s = 0
def find(n):
nonlocal s
if not n:
return None
if n.data == target:
return n
s += n.data
if n.data > target:
return find(n.left)
return find(n.right)
tn = find(root)
if not tn:
return -1
ans = float("-inf")
def dfs(n, sv=0):
nonlocal ans, s
if not n:
return
sv += n.data
if not n.left and not n.right:
ans = max(ans, s - sv)
else:
dfs(n.left, sv)
dfs(n.right, sv)
dfs(tn.left)
dfs(tn.right)
return s if ans == float("-inf") else ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NONE IF VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF NUMBER IF VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
temp = root
sum = 0
def solve(root):
if root == None:
return 1000000000.0
if root.left == None and root.right == None:
return root.data
left, right = solve(root.left), solve(root.right)
return root.data + min(left, right)
while temp:
if temp.data == target:
mn = solve(temp)
return sum - mn + target
elif temp.data < target:
sum += temp.data
temp = temp.right
else:
sum += temp.data
temp = temp.left
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def fun2(root):
if root.left and root.right:
leftmin, leftmax = fun2(root.left)
rightmin, rightmax = fun2(root.right)
return (
min([leftmin, leftmax, rightmin, rightmax]) + root.data,
max([leftmin, leftmax, rightmin, rightmax]) + root.data,
)
elif root.left:
leftmin, leftmax = fun2(root.left)
return root.data + leftmin, root.data + leftmax
elif root.right:
rightmin, rightmax = fun2(root.right)
return root.data + rightmin, root.data + rightmax
else:
return root.data, root.data
def fun(root, target, s):
if not root:
return -1
if root.data == target:
if root.left and root.right:
downmin, downmax = fun2(root)
downmin -= root.data
downmax -= root.data
return max(s - downmin, s - downmax)
if root.left:
downmin, downmax = fun2(root.left)
return max(s - downmin, s - downmax)
if root.right:
downmin, downmax = fun2(root.right)
return max(s - downmin, s - downmax)
else:
return s
if target < root.data:
return fun(root.left, target, s + root.data)
else:
return fun(root.right, target, s + root.data)
return fun(root, target, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR LIST VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def __init__(self):
self.target_node = None
def maxDifferenceBST(self, root, target):
root_target_sum = self.findTargetNodeSum(root, target)
if self.target_node == None:
return -1
min_leaf_sum = self.findMinPathToLeaf(self.target_node) - target
return root_target_sum - min_leaf_sum
def findTargetNodeSum(self, node, target):
if node == None:
return 0
curr_val = node.data
if curr_val == target:
self.target_node = node
return 0
elif target < curr_val:
return curr_val + self.findTargetNodeSum(node.left, target)
else:
return curr_val + self.findTargetNodeSum(node.right, target)
def findMinPathToLeaf(self, node):
if node == None:
return float("inf")
if node.left == None and node.right == None:
return node.data
return node.data + min(
self.findMinPathToLeaf(node.left), self.findMinPathToLeaf(node.right)
) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR STRING IF VAR NONE VAR NONE RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def solve(self, root, t, sm):
if root == None:
return [False, sm, -1]
if root.data == t:
return [True, sm, root]
l = self.solve(root.left, t, sm + root.data)
if l[0] == True:
return l
r = self.solve(root.right, t, sm + root.data)
if r[0] == True:
return r
return [False, sm, -1]
def solve1(self, root, s, ans):
if root.left == None and root.right == None:
if s + root.data < ans:
ans = s + root.data
return ans
if root.left != None:
ans = self.solve1(root.left, s + root.data, ans)
if root.right != None:
ans = self.solve1(root.right, s + root.data, ans)
return ans
def maxDifferenceBST(self, root, target):
cal = self.solve(root, target, 0)
if cal[0] == False:
return -1
ans = pow(10, 9)
ans = self.solve1(cal[2], 0, ans)
return cal[1] - (ans - cal[2].data) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST NUMBER VAR NUMBER IF VAR VAR RETURN LIST NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER RETURN VAR RETURN LIST NUMBER VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
sm = 0
cur = root
lr = 1
while cur.data != target:
sm += cur.data
if cur.data > target:
if cur.left:
cur = cur.left
else:
return -1
elif cur.right:
cur = cur.right
else:
return -1
def mn_mx(node, mn=0, mx=0):
if not node.left and not node.right:
return mn, mx
x, y = float("inf"), -float("inf")
if node.left:
a, b = mn_mx(node.left, mn + node.left.data, mx + node.left.data)
x = min(x, min(a, b))
y = max(y, max(a, b))
if node.right:
a, b = mn_mx(node.right, mn + node.right.data, mx + node.right.data)
x = min(x, min(a, b))
y = max(y, max(a, b))
return x, y
mn, mx = mn_mx(cur)
return max(sm - mn, sm - mx) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR RETURN NUMBER IF VAR ASSIGN VAR VAR RETURN NUMBER FUNC_DEF NUMBER NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def search(self, root, target, ans):
if not root:
return None
if root.data == target:
return root
self.ans += root.data
if target > root.data:
return self.search(root.right, target, ans)
else:
return self.search(root.left, target, ans)
def dfs(self, node):
res = node.data
mn = 10**9
if node.left:
mn = min(mn, self.dfs(node.left))
if node.right:
mn = min(mn, self.dfs(node.right))
if mn == 10**9:
return res
else:
return res + mn
def maxDifferenceBST(self, root, target):
self.ans = 0
node = self.search(root, target, self.ans)
if node == None:
return -1
self.ans -= self.dfs(node) - node.data
return self.ans | CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
ans = -2147483648
def maxDifferenceBST(self, root, target):
res = 0
while root != None:
if root.data == target:
break
elif root.data < target:
res += root.data
root = root.right
else:
res += root.data
root = root.left
if root == None:
return -1
Solution.ans = -2147483648
if root.left == None and root.right == None:
return res
if root.left != None:
self.solve(root.left, 0, res)
if root.right != None:
self.solve(root.right, 0, res)
return Solution.ans
def solve(self, root, path_sum, res):
if root == None:
return
if root.left == None and root.right == None:
Solution.ans = max(Solution.ans, res - (path_sum + root.data))
return
self.solve(root.left, path_sum + root.data, res)
self.solve(root.right, path_sum + root.data, res) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NONE IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER IF VAR NONE VAR NONE RETURN VAR IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def tillTarget(root):
nonlocal next_root
if root:
if root.data == target:
next_root = root
return root.data
if target > root.data:
return root.data + tillTarget(root.right)
else:
return root.data + tillTarget(root.left)
else:
return float("-inf")
def toLeaf(root):
if root == None:
return float("inf")
if root.left == root.right == None:
return root.data
return root.data + min(toLeaf(root.left), toLeaf(root.right))
next_root = None
sol = tillTarget(root)
if next_root == None:
return -1
return sol - toLeaf(next_root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR STRING IF VAR VAR NONE RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
sum1 = [0] * 1
node = [0] * 1
def fun(root):
if root == None:
return
if root.data == target:
node[0] = root
return
sum1[0] += root.data
if root.data > target:
fun(root.left)
else:
fun(root.right)
fun(root)
if node[0] == 0:
return -1
sum2 = [10**10]
def fun1(root, s):
if root == None:
return
if root.left == None and root.right == None:
sum2[0] = min(sum2[0], s + root.data)
fun1(root.left, s + root.data)
fun1(root.right, s + root.data)
fun1(node[0], -node[0].data)
return sum1[0] - sum2[0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def getsums(self, node, presum):
if node.left is None and node.right is None:
return [presum + node.data]
ans = []
if node.left is not None:
ans += self.getsums(node.left, presum + node.data)
if node.right is not None:
ans += self.getsums(node.right, presum + node.data)
return ans
def maxDifferenceBST(self, root, target):
cur = root
s1 = 0
while cur and cur.data != target:
s1 += cur.data
if cur.data < target:
cur = cur.right
else:
cur = cur.left
if cur is None:
return -1
s2 = self.getsums(cur, 0)
return s1 + target - min(s2) | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN LIST BIN_OP VAR VAR ASSIGN VAR LIST IF VAR NONE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def solve(self, root, sumval):
if root is None:
return
if root.left is None and root.right is None:
self.res = max(self.res, self.lsum - (sumval + root.data))
return
sumval = sumval + root.data
self.solve(root.left, sumval)
self.solve(root.right, sumval)
def findlsum(self, root, target):
if root is None:
return
if root.data == target:
return root
self.lsum = self.lsum + root.data
if root.data < target:
return self.findlsum(root.right, target)
else:
return self.findlsum(root.left, target)
def maxDifferenceBST(self, root, target):
self.lsum = 0
self.res = -(10**5)
troot = self.findlsum(root, target)
if troot is None:
return -1
if troot.left is not None:
self.solve(troot.left, 0)
if troot.right is not None:
self.solve(troot.right, 0)
if self.res == -(10**5):
return self.lsum
return self.res | CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP NUMBER NUMBER RETURN VAR RETURN VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def rootToTarg(self, root, target, sum1, targ):
if not root:
return
if root.data == target:
targ[0] = root
return
sum1[0] += root.data
if root.data > target:
self.rootToTarg(root.left, target, sum1, targ)
else:
self.rootToTarg(root.right, target, sum1, targ)
def dfs(self, targ):
if not targ:
return float("inf")
if not targ.left and not targ.right:
return targ.data
left, right = float("inf"), float("inf")
if targ.left:
left = self.dfs(targ.left)
if targ.right:
right = self.dfs(targ.right)
return targ.data + min(left, right)
def maxDifferenceBST(self, root, target):
if not root:
return -1
sum1 = [0]
targ = [None]
self.rootToTarg(root, target, sum1, targ)
if not targ[0]:
return -1
if not targ[0].left and not targ[0].right:
return sum1[0]
return sum1[0] - min(self.dfs(targ[0].left), self.dfs(targ[0].right)) | CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
def min_val(node):
if not node.left and not node.right:
return node.data
cur = 10000000.0
if node.left:
cur = min(cur, min_val(node.left))
if node.right:
cur = min(cur, min_val(node.right))
return node.data + cur
s = 0
ptr = root
while True:
if not ptr:
return -1
if target == ptr.data:
break
elif target < ptr.data:
s += ptr.data
ptr = ptr.left
else:
s += ptr.data
ptr = ptr.right
diff = min_val(ptr)
return s - diff + target | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def maxDifferenceBST(self, root, target):
l = []
def dfs(root, target, s):
if root == None:
return 0
if root.data == target:
l.append(root)
l.append(s + root.data)
return
dfs(root.left, target, s + root.data)
dfs(root.right, target, s + root.data)
dfs(root, target, 0)
if len(l) == 0:
return -1
def maxdiff(root, s):
if root == None:
return -(10**9)
if root.left == None and root.right == None:
return s - root.data
return max(
maxdiff(root.left, s - root.data), maxdiff(root.right, s - root.data)
)
return maxdiff(l[0], l[1]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN BIN_OP NUMBER NUMBER IF VAR NONE VAR NONE RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
You are given a Binary Search Tree and a target value. You must find the maximum difference between the sum of node data from root to target and from target to a child leaf node (target exclusive). Initially, you are at the root node.
Note: If the target node is not present in the tree then return -1.
Example 1:
Input:
Target = 20
Output: 10
Explanation: From root to target the sum of node data is 25 and from target to the children leaf nodes the sums of the node data are 15 and 22. So, the maximum difference will be (25-15) = 10.
Example 2:
Input:
Target = 50
Output: -1
Explanation: The target node is not present in the tree.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxDifferenceBST() which takes BST(you are given the root node of the BST ) and target as input, and returns an interger value as the required answer. If the target is not present in the BST then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(H), H - Height of the Tree.
Constraints:
1 <= n < 10^4
1 <= node.data < 10^5
1 <= target < 10^5 | class Solution:
def __init__(self):
self.targetSum = 0
self.childSum = float("inf")
self.targetNode = None
def findTarget(self, node, target):
while node:
self.targetSum += node.data
if target < node.data:
node = node.left
else:
node = node.right
if node and node.data == target:
self.targetNode = node
break
def leafTraversal(self, node, curr_sum):
if not node:
return curr_sum
self.leafTraversal(node.left, curr_sum + node.data)
self.leafTraversal(node.right, curr_sum + node.data)
if not node.left and not node.right:
self.childSum = min(self.childSum, curr_sum + node.data)
def maxDifferenceBST(self, root, target):
if not root:
return -1
if target == root.data:
self.targetNode = root
else:
self.findTarget(root, target)
if self.targetNode == None:
return -1
self.leafTraversal(self.targetNode.left, 0)
self.leafTraversal(self.targetNode.right, 0)
if not self.targetNode.left and not self.targetNode.right:
self.childSum = 0
return self.targetSum - self.childSum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FUNC_DEF WHILE VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR RETURN VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR |
Akku have solved many problems, she is genius. One day her friend gave her an Array of size n and asked her to perform
some queries of following type:
Each query consists of three integers
1 A B : Update the Array at index A by value B
2 A B : if the subarray from index A to B (both inclusive) is
1. Both increasing(Non-decreasing) and decreasing(Non-increasing) print -1
2. Only increasing(Non-decreasing) print 0
3. Only decreasing(Non-increasing) print 1
4. Neither increasing nor decreasing print -1
Akku needs your help, can you help her.
Example 1:
Input: nums = {1,5,7,4,3,5,9},
Queries = {{2,1,3},{1,7,4},{2,6,7}}
Output: {0,1}
Explanation: For the 1st query given :
A = 1, B = 3. From 1 to 3(1,5,7) elements
are in increasing order. So answer is 0.
For the 2nd query we have to update the 7th
element of the array by 4. So new updated array
will be {1,5,7,4,3,5,4}
For the 3rd query A = 6, B = 7. From 6 to 7
(5, 4) elements are in descending order. So
answer is 1.
Your Task:
You don't need to read or print anything. Your task is to complete the function solveQueries() which takes nums and Queries as input parameter and returns a list containing the answer for the 2nd type of query.
Expected Time Comeplxity: O(n*log(n))
Expected Space Comeplxity: O(n)
Constraints:
1 <= n <= 10^{4}
1 <= nums[i] <= 10^{4}
1 <= No. of queries <= 10^{4} | class Solution:
def algo(self, a):
l = list(a)
l.sort()
l1 = l[::-1]
if len(a) == 1:
return -1
if a == l:
return 0
if a == l1:
return 1
return -1
def solveQueries(self, nums, q):
ans = []
for i in range(len(q)):
l = q[i]
if l[0] == 1:
nums[l[1] - 1] = l[2]
elif l[0] == 2:
a = nums[l[1] - 1 : l[2]]
ans.append(self.algo(a))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR |
Akku have solved many problems, she is genius. One day her friend gave her an Array of size n and asked her to perform
some queries of following type:
Each query consists of three integers
1 A B : Update the Array at index A by value B
2 A B : if the subarray from index A to B (both inclusive) is
1. Both increasing(Non-decreasing) and decreasing(Non-increasing) print -1
2. Only increasing(Non-decreasing) print 0
3. Only decreasing(Non-increasing) print 1
4. Neither increasing nor decreasing print -1
Akku needs your help, can you help her.
Example 1:
Input: nums = {1,5,7,4,3,5,9},
Queries = {{2,1,3},{1,7,4},{2,6,7}}
Output: {0,1}
Explanation: For the 1st query given :
A = 1, B = 3. From 1 to 3(1,5,7) elements
are in increasing order. So answer is 0.
For the 2nd query we have to update the 7th
element of the array by 4. So new updated array
will be {1,5,7,4,3,5,4}
For the 3rd query A = 6, B = 7. From 6 to 7
(5, 4) elements are in descending order. So
answer is 1.
Your Task:
You don't need to read or print anything. Your task is to complete the function solveQueries() which takes nums and Queries as input parameter and returns a list containing the answer for the 2nd type of query.
Expected Time Comeplxity: O(n*log(n))
Expected Space Comeplxity: O(n)
Constraints:
1 <= n <= 10^{4}
1 <= nums[i] <= 10^{4}
1 <= No. of queries <= 10^{4} | class Solution:
def solveQueries(self, nums, Queries):
ans = []
for q, A, B in Queries:
if q == 1:
nums[A - 1] = B
if q == 2:
s = set()
for x0, x1 in zip(nums[A - 1 : B - 1], nums[A:B]):
r = "<" if x0 < x1 else ">" if x0 > x1 else "="
s.add(r)
r = -1 if not s else 0 if ">" not in s else 1 if "<" not in s else -1
ans.append(r)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | import sys
class Solution:
def find(self, root):
if not root:
return -sys.maxsize - 1
if not root.left and not root.right:
return root.data
left = self.find(root.left)
right = self.find(root.right)
return left + root.data if left > right else right + root.data
def maxPathSum(self, root):
if not root:
return 0
return self.find(root) | IMPORT CLASS_DEF FUNC_DEF IF VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def dfs(root):
if not root:
return -1000
else:
l = dfs(root.left)
r = dfs(root.right)
if l == -1000 and r == -1000:
return root.data
else:
return max(l, r) + root.data
return dfs(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
return self.mS(root)
def mS(self, root):
if root == None:
return -32676
if root.left == None and root.right == None:
return root.data
l = self.mS(root.left)
r = self.mS(root.right)
return root.data + max(l, r) | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root is None:
return 0
if root.left is None:
return root.data + self.maxPathSum(root.right)
if root.right is None:
return root.data + self.maxPathSum(root.left)
return root.data + max(self.maxPathSum(root.left), self.maxPathSum(root.right)) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def helper(root):
if not root:
return -float("inf")
if not root.left and not root.right:
return root.data
l = root.data + helper(root.left)
r = root.data + helper(root.right)
return max(l, r)
return helper(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
ans = []
if root is None:
return 0
def solve(root, x, ans):
if root.left is None and root.right is None:
x = x + root.data
ans.append(x)
else:
if root.right:
solve(root.right, x + root.data, ans)
if root.left:
solve(root.left, x + root.data, ans)
solve(root, 0, ans)
return max(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NONE RETURN NUMBER FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
summ = [0]
def sumpath(root, summ):
ls = -99999
rs = -99999
if not root.left and not root.right:
summ[0] = root.data
return summ[0]
if root.left:
ls = sumpath(root.left, summ)
if root.right:
rs = sumpath(root.right, summ)
summ[0] = max(ls, rs) + root.data
return summ[0]
sumpath(root, summ)
return summ[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR RETURN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def util(self, root):
if not root:
return -(10**7)
ls = self.util(root.left)
rs = self.util(root.right)
mx = max(ls, rs)
if mx == -(10**7):
mx = 0
return root.data + mx
def maxPathSum(self, root):
return self.util(root) | CLASS_DEF FUNC_DEF IF VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def helper(root):
if not root:
return -999999999
if root.left == None and root.right == None:
return root.data
l = helper(root.left)
r = helper(root.right)
return root.data + max(l, r)
a = helper(root)
return a | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def root_node(root):
nonlocal sum1, maximum
if root == None:
return None
sum1 += root.data
if root.left == None and root.right == None and sum1 > maximum:
maximum = sum1
if root_node(root.left) or root_node(root.right):
return True
sum1 -= root.data
return False
sum1, maximum = 0, -9999
root_node(root)
return maximum | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE VAR VAR IF VAR NONE VAR NONE VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def helper(self, node, sum1):
if node is None:
return
if not node.left and not node.right:
self.max1 = max(self.max1, sum1 + node.data)
self.helper(node.left, sum1 + node.data)
self.helper(node.right, sum1 + node.data)
def maxPathSum(self, root):
self.max1 = float("-inf")
self.helper(root, 0)
return self.max1 | CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def find_max(node):
if not node:
return None
if node:
right_max = find_max(node.right)
left_max = find_max(node.left)
if right_max and left_max:
max_node = max(right_max, left_max) + node.data
elif right_max and not left_max:
max_node = right_max + node.data
elif not right_max and left_max:
max_node = left_max + node.data
elif not right_max and not left_max:
max_node = node.data
return max_node
max_value = find_max(root)
return max_value | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def fun(root, s):
if root.left == None and root.right == None:
maxi[0] = max(maxi[0], s + root.data)
return
if root.left:
fun(root.left, s + root.data)
if root.right:
fun(root.right, s + root.data)
maxi = [-(2**32)]
fun(root, 0)
return maxi[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
cr, m = 0, float("-inf")
def maximal(r, cr):
nonlocal m
if not r:
return 0
cr += r.data
if not r.left and not r.right:
m = max(m, cr)
maximal(r.left, cr)
maximal(r.right, cr)
maximal(root, cr)
return m | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
global maxsum
maxsum = -999999
def S(root, sum):
global maxsum
if not root:
return 0
if root.left == None and root.right == None and sum + root.data > maxsum:
maxsum = sum + root.data
S(root.left, sum + root.data)
S(root.right, sum + root.data)
S(root, 0)
return maxsum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR NONE VAR NONE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def f(root):
if not root:
return 0
left = f(root.left)
right = f(root.right)
new_left = max(0, left)
new_right = max(0, right)
if new_left == 0 and new_right == 0:
if root.left and root.right:
return max(left, right) + root.data
elif root.left and not root.right:
return left + root.data
elif not root.left and root.right:
return right + root.data
elif not root.left and not root.right:
return root.data
return max(new_left, new_right) + root.data
return f(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root is None:
return 0
if root.left is None and root.right is None:
return root.data
leftSumTree = self.maxPathSum(root.left)
rightSumTree = self.maxPathSum(root.right)
if leftSumTree == 0:
return root.data + rightSumTree
elif rightSumTree == 0:
return root.data + leftSumTree
else:
return root.data + max(leftSumTree, rightSumTree) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def pathToTarget(self, root, target, q):
if root is None:
return False
self.sm += root.data
if root.left is None and root.right is None and self.sm > self.maxsum:
self.maxsum = self.sm
if self.pathToTarget(root.left, target, q) or self.pathToTarget(
root.right, target, q
):
q.append(root.data)
return True
self.sm -= root.data
return False
def postorder(self, root, q):
if root is None:
return
self.sm += root.data
if root.left is None and root.right is None:
if self.sm > self.maxsum:
self.maxsum = self.sm
q.append(root)
self.target = root
self.postorder(root.left, q)
self.postorder(root.right, q)
def maxPathSum(self, root):
q = []
self.target = None
self.rootd = root
self.maxsum = -9999
self.sm = 0
q = []
self.pathToTarget(root, self.target, q)
return self.maxsum | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER VAR VAR IF VAR NONE VAR NONE VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN VAR VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root == None:
return float("-inf")
if root.left == None and root.right == None:
return root.data
maxleft = self.maxPathSum(root.left)
maxright = self.maxPathSum(root.right)
return root.data + max(maxleft, maxright) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR STRING IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root.left is None and root.right is None:
return root.data
elif root.left is not None and root.right is None:
return root.data + self.maxPathSum(root.left)
elif root.left is None and root.right is not None:
return root.data + self.maxPathSum(root.right)
else:
return max(
root.data + self.maxPathSum(root.left),
root.data + self.maxPathSum(root.right),
) | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def find(root, sm=0):
if not root:
return float("-inf")
if not root.left and not root.right:
return root.data
return root.data + max(find(root.left), find(root.right))
return find(root) | CLASS_DEF FUNC_DEF FUNC_DEF NUMBER IF VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def __init__(self):
self.l = []
self.ans = -(10**9) + 9
def maxPathSum(self, root):
def helper(root):
if root is None:
return
self.l.append(root.data)
if root.left is None and root.right is None:
su = 0
for i in self.l:
su += i
self.ans = max(self.ans, su)
helper(root.left)
helper(root.right)
self.l.pop()
helper(root)
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def __init__(self):
self.summ = []
def trav(self, root, s):
if root.left == None and root.right == None:
self.summ.append(s + root.data)
return
if root.left != None:
self.trav(root.left, s + root.data)
if root.right != None:
self.trav(root.right, s + root.data)
def maxPathSum(self, root):
self.trav(root, 0)
return max(self.summ) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE VAR NONE EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def getPaths(self, root, temp, result):
if root:
temp.append(root.data)
self.getPaths(root.left, temp, result)
self.getPaths(root.right, temp, result)
if root.left == None and root.right == None:
temp_ = sum(temp)
result.append(temp_)
temp.pop()
def maxPathSum(self, root):
temp = []
result = []
self.getPaths(root, temp, result)
return max(result) | CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root is None:
return float("-inf")
if root.left is None and root.right is None:
return root.data
return root.data + max(self.maxPathSum(root.left), self.maxPathSum(root.right)) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR STRING IF VAR NONE VAR NONE RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def Postorder(self, root):
if root == None:
return -(2**63)
l = self.Postorder(root.left)
r = self.Postorder(root.right)
if root.left == None and root.right == None:
return root.data
return max(l, r) + root.data
def maxPathSum(self, root):
ans = self.Postorder(root)
return ans | CLASS_DEF FUNC_DEF IF VAR NONE RETURN BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
temp = root
val = []
s = 0
def ino(temp, s):
if temp == None:
return
if temp.left == None and temp.right == None:
s += temp.data
val.append(s)
return
s += temp.data
l = ino(temp.left, s)
r = ino(temp.right, s)
ino(temp, s)
maxi = max(val)
return maxi | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def find(root, sm):
if root:
if not root.left and not root.right:
ans[0] = max(ans[0], sm + root.data)
sm += root.data
find(root.left, sm)
find(root.right, sm)
sm -= root.data
return
ans = [-100000000]
find(root, 0)
return ans[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
msum = -(10**6 + 1)
target = None
l = []
def solve(self, root, csum):
if root == None:
return
csum += root.data
if root.left == None and root.right == None:
if csum > self.msum:
self.msum = csum
self.target = root
self.solve(root.left, csum)
self.solve(root.right, csum)
def maxPathSum(self, root):
self.solve(root, 0)
return self.msum | CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN VAR VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root.left != None and root.right == None:
return root.data + self.maxPathSum(root.left)
if root.left == None and root.right != None:
return root.data + self.maxPathSum(root.right)
if root.left == root.right == None:
return root.data
w1 = self.maxPathSum(root.left)
w2 = self.maxPathSum(root.right)
return root.data + max(w1, w2) | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root == None:
return -999999999
if root.left == None and root.right == None:
return root.data
left = self.maxPathSum(root.left)
right = self.maxPathSum(root.right)
sum = max(left, right) + root.data
return sum | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if not root:
return 0
def helper(root):
if not root:
return 0
if not root.left:
value = helper(root.right)
elif not root.right:
value = helper(root.left)
else:
value = max(helper(root.left), helper(root.right))
return value + root.data
return helper(root) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
self.maxi = -9868695968
self.helper(root, sum=0)
return self.maxi
def helper(self, root, sum):
if root == None:
return -1
sum += root.data
left = self.helper(root.left, sum)
right = self.helper(root.right, sum)
if root.left == None and root.right == None:
self.maxi = max(self.maxi, sum)
return max(left, right) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root == None:
return 0
ans = -(10**9)
notLeaf = False
if root.left:
notLeaf = True
ans = max(ans, self.maxPathSum(root.left))
if root.right:
notLeaf = True
ans = max(ans, self.maxPathSum(root.right))
return root.data + ans * notLeaf | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | import sys
class Solution:
def maxPathSum(self, root):
def psum(root, val):
nonlocal maxp
if root is None:
return
val += root.data
if root.left is None and root.right is None:
maxp = max(maxp, val)
psum(root.left, val) or psum(root.right, val)
val = 0
maxp = -sys.maxsize
psum(root, val)
return maxp | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | import sys
class Solution:
def maxPathSum(self, root):
import sys
def rec(root):
if root is None:
return -sys.maxsize
if root.left is None and root.right is None:
return root.data
return root.data + max(rec(root.left), rec(root.right))
return rec(root) | IMPORT CLASS_DEF FUNC_DEF IMPORT FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | import sys
class Solution:
def dfs(self, root, msum):
if root is None:
return msum
if root.right is None and root.left is None:
return root.data
lsum = self.dfs(root.left, msum) + root.data
rsum = self.dfs(root.right, msum) + root.data
msum = max(lsum, rsum, msum)
return msum
def maxPathSum(self, root):
return self.dfs(root, -sys.maxsize - 1) | IMPORT CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def solve(self, root, ans):
if root == None:
return
ans.append(root.data)
if root.left == None and root.right == None:
k = sum(ans)
self.ans = max(self.ans, k)
self.solve(root.left, ans)
self.solve(root.right, ans)
ans.pop()
def maxPathSum(self, root):
self.ans = float("-inf")
ans = []
self.solve(root, ans)
return self.ans | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
if root == None:
return 0
l = root.data + self.maxPathSum(root.left)
r = root.data + self.maxPathSum(root.right)
if l == root.data and r == root.data:
return root.data
elif l == root.data:
return r
elif r == root.data:
return l
else:
return max(l, r) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
res = -(10**6 + 1)
def maxPathSum(self, root):
self.maxPathSumUtil(0, root)
return self.res
def maxPathSumUtil(self, curSum, root):
if root == None:
return
curSum += root.data
if root.left == None and root.right == None:
if curSum > self.res:
self.res = curSum
return
self.maxPathSumUtil(curSum, root.left)
self.maxPathSumUtil(curSum, root.right) | CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def dfs(root, sm):
if not root:
return 0
sm = sm + root.data
if not root.left and not root.right:
return sm
if not root.left:
return dfs(root.right, sm)
if not root.right:
return dfs(root.left, sm)
return max(dfs(root.left, sm), dfs(root.right, sm))
return dfs(root, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR RETURN FUNC_CALL VAR VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
return self.path_sum(root, 0)
def path_sum(self, node, total):
total += node.data
if node.left == None and node.right == None:
return total
if node.left == None and node.right != None:
return self.path_sum(node.right, total)
if node.right == None and node.left != None:
return self.path_sum(node.left, total)
return max(self.path_sum(node.right, total), self.path_sum(node.left, total)) | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def __init__(self):
self.max_sum = -float("inf")
self.target_node = None
def _get_target_leaf(self, node, current_sum):
if not node:
return
current_sum += node.data
if not node.left and not node.right:
if current_sum > self.max_sum:
self.max_sum = current_sum
self.target_node = node
return
self._get_target_leaf(node.left, current_sum)
self._get_target_leaf(node.right, current_sum)
def maxPathSum(self, root):
if not root:
return 0
self._get_target_leaf(root, 0)
return self.max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FUNC_DEF IF VAR RETURN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def __init__(self):
self.c = float("-inf")
def check(self, a, s):
if a == None:
return
s += a.data
if a.left == None and a.right == None:
self.c = max(self.c, s)
self.check(a.left, s)
self.check(a.right, s)
def maxPathSum(self, root):
self.check(root, 0)
return self.c | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE RETURN VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def rec(root, curr, max_):
if not root:
return max_
curr += root.data
if not root.right and not root.left:
max_ = max(curr, max_)
return max_
temp = max(rec(root.right, curr, max_), rec(root.left, curr, max_))
return temp
max_ = rec(root, 0, -100000000)
return max_ | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
res = float("-inf")
def rec(root, sum):
nonlocal res
if root.left == None and root.right == None:
res = max(res, sum[0] + root.data)
return
if root.left:
rec(root.left, [sum[0] + root.data])
if root.right:
rec(root.right, [sum[0] + root.data])
rec(root, [0])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR LIST BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR |
Given a Binary Tree, find the maximum sum path from a leaf to root.
Example 1:
Input:
1
/ \
2 3
Output:
4
Explanation:
Following the path 3 -> 1, results in a
sum of 4, which is the maximum path sum
from leaf to root for the given tree.
Example 2:
Input:
10
/ \
-2 7
/ \
8 -4
Output:
17
Explanation :
Following the path 7 -> 10, results in a
sum of 17, which is the maximum path sum
from leaf to root for the given tree.
Your task :
You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum.
Expected Time Complexity: O(n) , where n = number of nodes
Expected Auxiliary Space: O(1)
Constraints :
1 <= Number of nodes <= 10^5
-10^{6} <= max sum path <= 10^{6} | class Solution:
def maxPathSum(self, root):
def solve(root):
if root is None:
return 0
left = solve(root.left)
right = solve(root.right)
if left == 0:
return root.data + right
elif right == 0:
return root.data + left
else:
return root.data + max(left, right)
ans = solve(root)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.