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
<!-- Describe your first thoughts on how to solve this problem. -->
12
-
The problem involves finding the maximal rectangle containing only 1s in a binary matrix. To solve this problem efficiently, I can utilize the concept of histograms. For each row in the matrix, I can treat it as the base of a histogram and calculate the height of bars in the histogram based on the consecutive 1s in that row. Then, I can apply the largest rectangle in histogram algorithm to find the maximal rectangle for each row. Finally, I return the maximum area among all rows as the result.
13
-
12
+
The goal is to find the sum of all left leaves in a binary tree. A left leaf is a leaf node that is the left child of its parent.
14
13
# Approach
15
14
<!-- Describe your approach to solving the problem. -->
16
15
17
-
- I initialized a variable to store the maximal rectangle area.
18
-
- Iterate through each row in the matrix:
19
-
- Update the histogram of heights for each row. Treat 1s as bars and consecutive 1s as increasing heights in the histogram.
20
-
- Calculate the maximal rectangle area using the largest rectangle in histogram algorithm for the current histogram.
21
-
- Update the maximal rectangle area if the current area is greater.
22
-
- Return the maximal rectangle area as the result.
23
-
24
-
##### Largest Rectangle in Histogram Algorithm :
25
-
- I nitialized a stack to store the indices of histogram bars.
26
-
- Iterated through each bar in the histogram:
27
-
- If the stack is empty or the current bar's height is greater than or equal to the height of the bar at the top of the stack, push the index of the current bar onto the stack.
28
-
- If the current bar's height is less than the height of the bar at the top of the stack, keep popping bars from the stack until the stack is empty or the height of the bar at the top of the stack is less than the current bar's height. For each popped bar, calculate its area using the popped height as the height of the rectangle and the difference between the current index and the index of the bar at the top of the stack as the width of the rectangle. Update the maximal rectangle area if the calculated area is greater.
29
-
- After iterating through all bars, if there are bars left in the stack, repeat step 2 for each remaining bar.
30
-
- Returned the maximal rectangle area calculated.
16
+
- I started with the root node.
17
+
- If the current node is null, returned 0.
18
+
- Check if the left child of the current node exists and if it is a leaf node (i.e., it has no left or right children).
19
+
- If the left child is a leaf node, add its value to the result and continue recursively with the right child.
20
+
- If the left child is not a leaf node, recursively call the function on both the left and right children.
21
+
- Returned the sum obtained from the left subtree and the right subtree.
31
22
32
23
---
33
24
Have a look at the code , still have any confusion then please let me know in the comments
34
25
Keep Solving.:)
35
26
36
27
# Complexity
37
-
- Time complexity : $O(m*n)$
28
+
- Time complexity : $O(n)$
38
29
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
39
-
$m$ : number of rows
40
-
41
-
$n$ : number of columns
30
+
$n$ : number of nodes in the binary tree
42
31
- Space complexity : $O(n)$
43
32
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
44
33
45
34
# Code
46
35
```
47
-
class Solution {
48
-
49
-
// Method to find the maximal rectangle area in a given matrix
0 commit comments