You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
intmaxPathSum(TreeNode? root) {
int maxi =-1000;
List<TreeNode> stack = [];
TreeNode? pre = root;
TreeNode? cur = root;
TreeNode? old =null;
while (stack.isNotEmpty || cur !=null) {
while (cur !=null) {
stack.add(cur);
cur = cur.left;
}
if (stack.isNotEmpty) {
pre = stack.last;
cur = pre.right;
if (cur ==null|| cur == old) {
stack.removeLast();
int left = pre.left ==null?0: pre.left!.val;
int right = cur ==null?0: cur.val;
maxi =max(
maxi,
max(
pre.val,
max(pre.val + left,
max(pre.val + right, pre.val + left + right))));
pre.val =max(pre.val, max(pre.val + right, pre.val + left));
old = pre;
cur =null;
} else
old = cur;
}
}
return maxi;
}
}