Amazon Coding Interview Question - Sum of Left Leaves (LeetCode)

  Рет қаралды 4,687

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 12
@user-rf4vc7mt4d
@user-rf4vc7mt4d 2 жыл бұрын
This was my attempt/solution before watching this. It's so cool being able to do it myself and see a completely different way of coding this up class Solution { private int dfs(TreeNode root, char direction) { if (root == null) return 0; if (root.left == null && root.right == null) { // System.out.println(" Found leaf: " + root.val); if (direction == 'L') { return root.val; } else { return 0; } } return dfs(root.left, 'L') + dfs(root.right, 'R'); } public int sumOfLeftLeaves(TreeNode root) { return dfs(root.left, 'L') + dfs(root.right, 'R'); } }
@aimeewildstone4602
@aimeewildstone4602 3 жыл бұрын
I really appreciate you going over recursive and iterative solutions along with clear explanations. I’m looking forward to more of your videos!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
More to come!
@eldavimost
@eldavimost 2 жыл бұрын
Great explanation! What program do you use to create the graphics and paint on it?
@B-Billy
@B-Billy 3 жыл бұрын
Thank you... clear and detailed explanation
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad it was helpful!
@saulgoodman6710
@saulgoodman6710 Жыл бұрын
if you add the isleafnode() check for the right nodes before adding to the queue it would me more optimal right? because if right node is leaf node we dont need to add it to the queue. Let me know ur views
@abhinavghosh725
@abhinavghosh725 4 жыл бұрын
hey man!good video! one question ,why cant i use global variable, we are setting sum=0 in every recursion call why cant we do like this: class Solution { int sum=0; public int sumOfLeftLeaves(TreeNode root) { if(root==null)return 0; // int sum=0; if(root.left!=null) { if(root.left.left==null && root.left.right==null) { sum+=root.left.val; } else { sum+=sumOfLeftLeaves(root.left); } } sum+=sumOfLeftLeaves(root.right); return sum; } } i have been having problems with tree questions from a very long time! can you please make a video explaining how actually recursion works in a tree! its a request man ! please do a specific video for that! thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Hey! Thanks for the question. The reason why we can't set a global variable in the recursive approach is because we are building the sum bottom-up. If we have a global variable, the line `sum += root.left.val` will effect recursive calls below it if that makes sense. With the following input `[3,9,20,null,null,15,7]`, your code outputs `57` when it should be outputting `24`. Hope that makes sense and thank you so much watching!
@stardriver8660
@stardriver8660 3 жыл бұрын
class Solution { public int sumOfLeftLeaves(TreeNode root) { return helper(root,"root"); } public int helper(TreeNode root,String mark){ if(root == null)return 0; if(root.left == null && root.right == null){ if(mark.equals("left"))return root.val; } return helper(root.left,"left") + helper(root.right,"right"); } } How do you like my solution?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Looks good
FACEBOOK CODING INTERVIEW QUESTION - LOWEST COMMON ANCESTOR
10:22
AlgosWithMichael
Рет қаралды 9 М.
Amazon Coding Interview Question - Number of Distinct Islands
17:43
AlgosWithMichael
Рет қаралды 26 М.
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
小路飞还不知道他把路飞给擦没有了 #路飞#海贼王
00:32
路飞与唐舞桐
Рет қаралды 87 МЛН
Binary Tree Maximum Path Sum - DFS - Leetcode 124 - Python
15:19
Amazon Coding Interview Question - Clone Graph (LeetCode)
13:56
AlgosWithMichael
Рет қаралды 33 М.
What Is a Binary Heap?
8:45
Spanning Tree
Рет қаралды 196 М.
Sum of Left Leaves - LeetCode 404 - Python
13:48
DEEPTI TALESRA
Рет қаралды 1,1 М.
Longest Increasing Path in a Matrix (DFS + Memoization)
18:47
AlgosWithMichael
Рет қаралды 19 М.
AVL Trees & Rotations (Self-Balancing Binary Search Trees)
20:38
Back To Back SWE
Рет қаралды 351 М.
Amazon Coding Interview Question - Reverse Vowels In A String [LeetCode]
10:38