
Binary Tree Algorithms for Technical Interviews - Full Course
video description
fun maxRootDFSIterative(root: TreeNode?):Int-
var maxSumPath = Int.MIN_VALUE
val stack = ArrayDeque()
root?.let - stack.push(it) -
while (stack.isNotEmpty())-
val currentNode = stack.pop()
currentNode.right?.let -
it.value += currentNode.value
stack.push(it)
-
currentNode.left?.let -
it.value+=currentNode.value
stack.push(it)
-
if (currentNode.right==null && currentNode.left== null)-
maxSumPath = max(maxSumPath, currentNode.value)
-
-
return maxSumPath
-
Date: 2022-03-14
Related videos
Comments and reviews: 9
Myogatino
I love your videos, super clear and intuitive! Thank you! Also love Structy!
But in this video, there's a small mistake during the explanation of Depth first values. Around 32:26, the leftValues should be ['b', 'd'] and the rightValues should be ['e', 'c', 'f']. Of course the final result will be the same. Nothing wrong with the code.
Can't wait for more of your algorithm videos!
reply
I love your videos, super clear and intuitive! Thank you! Also love Structy!
But in this video, there's a small mistake during the explanation of Depth first values. Around 32:26, the leftValues should be ['b', 'd'] and the rightValues should be ['e', 'c', 'f']. Of course the final result will be the same. Nothing wrong with the code.
Can't wait for more of your algorithm videos!
reply
Anshul
I'm getting wrong output for BFS. The output is similar to DFS. Can anyone point out the error?
def breadthfirst_iterative(root):
queue=[root]
result1 = []
while len(queue)>0:
curr=queue.pop()
result1.append(curr.val)
if curr.left: queue.append(curr.left)
if curr.right: queue.append(curr.right)
print (result1)
breadthfirst_iterative(a)
reply
I'm getting wrong output for BFS. The output is similar to DFS. Can anyone point out the error?
def breadthfirst_iterative(root):
queue=[root]
result1 = []
while len(queue)>0:
curr=queue.pop()
result1.append(curr.val)
if curr.left: queue.append(curr.left)
if curr.right: queue.append(curr.right)
print (result1)
breadthfirst_iterative(a)
reply
Yogi
Thanks a lot dude! you made it so intuitive to solve these tree problems, just a suggestion I think last problem can be also be solved like this:
function maxPathSum(node) --
if (!node) return -Infinity;-
return node.val + Math.max(0, maxPathSum(node.left), maxPathSum(node.right));-
-
reply
Thanks a lot dude! you made it so intuitive to solve these tree problems, just a suggestion I think last problem can be also be solved like this:
function maxPathSum(node) --
if (!node) return -Infinity;-
return node.val + Math.max(0, maxPathSum(node.left), maxPathSum(node.right));-
-
reply
Alvin
Hey Programmers! Thanks for watching. Head to Structy for more data structure and algorithm tutorials from me. Link available in the description. There you'll find animated explanations and video walkthroughs for data structures and algorithms in JavaScript, Python, and C++.
reply
Hey Programmers! Thanks for watching. Head to Structy for more data structure and algorithm tutorials from me. Link available in the description. There you'll find animated explanations and video walkthroughs for data structures and algorithms in JavaScript, Python, and C++.
reply
gagag96
This was a great video and very well organized. I finished watching the whole video while coding the problems on Visual Studio Code in Python and now feel I have a good grasp of BFS, DFS, especially using recursion which I struggled with before. Thank you Alvin!
reply
This was a great video and very well organized. I finished watching the whole video while coding the problems on Visual Studio Code in Python and now feel I have a good grasp of BFS, DFS, especially using recursion which I struggled with before. Thank you Alvin!
reply
Anna
I love that he goes over all the common mistakes people often make on the first try! It's very normalizing to see him show those mistakes, makes me feel much better lol, and also the explanations of those mistakes dig deeper and improve our understanding.
reply
I love that he goes over all the common mistakes people often make on the first try! It's very normalizing to see him show those mistakes, makes me feel much better lol, and also the explanations of those mistakes dig deeper and improve our understanding.
reply
Im
Hi bro! I followed the whole video until the end and finally did the last exercise by my own and it worked! Thanks for the lessons , i love you and wish you a Merry Christmas -
reply
Hi bro! I followed the whole video until the end and finally did the last exercise by my own and it worked! Thanks for the lessons , i love you and wish you a Merry Christmas -
reply
Sami
Covered this video in one night :D
Amazing by the speed and quality of teaching from Alvin.
Really helpful stuff, and recommend Alvin for getting prepared for DS&A.
reply
Covered this video in one night :D
Amazing by the speed and quality of teaching from Alvin.
Really helpful stuff, and recommend Alvin for getting prepared for DS&A.
reply
Rene
I think this looks better for the last one
def maxPath(root):-
if root == None: return 0-
return root.val + max([maxPath(root.right), maxPath(root.left)])
reply
I think this looks better for the last one
def maxPath(root):-
if root == None: return 0-
return root.val + max([maxPath(root.right), maxPath(root.left)])
reply
Add a review, comment















