|
1 | 1 | ---
|
2 |
| -description: 'Author: @heiheihang | https://leetcode.com/problems/sum-of-left-leaves/' |
| 2 | +description: 'Author: @heiheihang, @wingkwong, @jit | https://leetcode.com/problems/sum-of-left-leaves/' |
| 3 | +tags: [Tree, Depth-First Search, Breadth-First Search, Binary Tree] |
3 | 4 | ---
|
4 | 5 |
|
5 | 6 | # 0404 - Sum of Left Leaves (Easy)
|
@@ -38,27 +39,63 @@ Output: 0
|
38 | 39 |
|
39 | 40 | Following the template from [DFS Guide](../../tutorials/graph-theory/depth-first-search), we iterate to each node and check if it is a left leaf. If it is a left leaf, we return that value to its parent for cumulating the sum. The trick here is to add a `is_left` parameter to the `dfs` function.
|
40 | 41 |
|
| 42 | +<Tabs> |
| 43 | +<TabItem value="py" label="Python"> |
41 | 44 | <SolutionAuthor name="@heiheihang"/>
|
42 | 45 |
|
43 |
| -```python |
| 46 | +```py |
44 | 47 | def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
|
45 |
| - def dfs(node, isLeft): |
46 |
| - |
47 |
| - #skip when we reach the end of the path |
48 |
| - if(node is None): |
49 |
| - return 0 |
50 |
| - |
51 |
| - #if node is a leaf (no left and right) and is the left child |
52 |
| - #return its value for adding |
53 |
| - if(node.left is None and node.right is None and isLeft is True): |
54 |
| - return node.val |
55 |
| - |
56 |
| - #perform dfs on both children |
57 |
| - #set isLeft to true on left child |
58 |
| - #set isLeft to false on right child |
59 |
| - return dfs(node.left, True) + dfs(node.right, False) |
60 |
| - |
61 |
| - #root can be a leaf if it is the only node in the tree |
62 |
| - #but it is not a left leaf (no right/left) |
63 |
| - return dfs(root, False) |
| 48 | + def dfs(node, isLeft): |
| 49 | + # skip when we reach the end of the path |
| 50 | + if (node is None): return 0 |
| 51 | + # if node is a leaf (no left and right) and is the left child |
| 52 | + # return its value for adding |
| 53 | + if (node.left is None and node.right is None and isLeft is True): return node.val |
| 54 | + # perform dfs on both children |
| 55 | + # set isLeft to true on left child |
| 56 | + # set isLeft to false on right child |
| 57 | + return dfs(node.left, True) + dfs(node.right, False) |
| 58 | + # root can be a leaf if it is the only node in the tree |
| 59 | + # but it is not a left leaf (no right/left) |
| 60 | + return dfs(root, False) |
64 | 61 | ```
|
| 62 | + |
| 63 | +</TabItem> |
| 64 | + |
| 65 | + |
| 66 | +<TabItem value="cpp" label="C++"> |
| 67 | +<SolutionAuthor name="@wingkwong"/> |
| 68 | + |
| 69 | +```cpp |
| 70 | +class Solution { |
| 71 | +public: |
| 72 | + int sumOfLeftLeaves(TreeNode* root, bool left = false) { |
| 73 | + // use left flag to determine it is a left node |
| 74 | + // traverse each node and only return if left=true and no child branches |
| 75 | + if(!root) return 0; |
| 76 | + if(left && !root->left && !root->right) return root->val; |
| 77 | + return sumOfLeftLeaves(root->left, true) + sumOfLeftLeaves(root->right, false); |
| 78 | + } |
| 79 | +}; |
| 80 | +``` |
| 81 | +
|
| 82 | +</TabItem> |
| 83 | +
|
| 84 | +
|
| 85 | +<TabItem value="elixir" label="Elixir"> |
| 86 | +<SolutionAuthor name="@jit"/> |
| 87 | +
|
| 88 | +```elixir |
| 89 | +defmodule Solution do |
| 90 | + # Using a left/right flag: |
| 91 | + @spec sum_of_left_leaves(root :: TreeNode.t | nil) :: integer |
| 92 | + def sum_of_left_leaves(root), do: dfs(root, :neither) |
| 93 | +
|
| 94 | + defp dfs(nil, _flag), do: 0 |
| 95 | + defp dfs(%{val: v, left: nil, right: nil}, :left), do: v |
| 96 | + defp dfs(%{left: l, right: r}, _flag), do: |
| 97 | + dfs(l, :left) + dfs(r, |
| 98 | +``` |
| 99 | + |
| 100 | +</TabItem> |
| 101 | +</Tabs> |
0 commit comments