-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from DivyaGodayal/largest_val_each_row
Added article and animation for leetcode-515
- Loading branch information
Showing
21 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<p align="center"> | ||
<img src="../../Images/Largest-Value-in-Each-Tree-Row/main.png" width="600"> | ||
</p> | ||
|
||
--- | ||
### Solution 1: Level Order Traversal | ||
|
||
#### Motivation | ||
|
||
The problem statement gives a huge hint towards performing a `level order traversal` on the tree. Whenever we want to access elements in the tree one row or level at a time, level order traversal (or breadth first search) is usually the way to go. This question is no exception to this rule. Essentially, we will perform level order traversal and will simply keep a track of the maximum element in each level. | ||
|
||
#### Algorithm | ||
|
||
1. Initialize an empty queue, `Q`. Queue is the data structure that is used for performing the level order traversal. The logic that we will adopt is that the queue, at any point in time will contain *all* the elements belonging to a particular level. That ways, we can simply find out the maximum element for each level. | ||
2. Push the root node, `R` for the tree in the queue. Root node is the only node in the first level or level `0` of the tree and so that will be the maximum. | ||
3. We iterate until the queue becomes empty. In each iteration, we note the size of the queue. Let's call the size, `L`. This size would represent the number of nodes in the current level. We don't need to keep track of the level number here. We just need to find the maximum valued node in each level and add it to an answer array. | ||
4. For each of the `L` elements in the `Q`, we push their left and right children into the queue and also update a local maximum. Note that we use an additional internal loop for iterating over the `L` elements of the queue. This is in addition to the outermost `while` loop. This is done so that we can easily iterate level-by-level *without using any additional information for keeping track of the level*. | ||
|
||
<p align="center"> | ||
<img src="../../Images/Largest-Value-in-Each-Tree-Row/dry_run.gif" width="700"> | ||
</p> | ||
|
||
#### Complexity Analysis | ||
|
||
* Time Complexity: `O(N)` where `N` represents the number of nodes in the tree. The complexity for running breadth first search on a graph is `O(V + E)` where `V` are the number of vertices and `E` are the number of edges. For a binary tree, at most every node will have 2 children and hence, `E = kV`. So, the overall time complexity is `O(V) = O(N)`. | ||
* Space Complexity: `O(N)` which is the space occupied by the number of nodes on a particular level. For a complete binary tree, the maximum number of nodes will be in the last level and all of them will be in the queue at the same time. | ||
|
||
#### Link to OJ | ||
|
||
https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/ | ||
|
||
--- | ||
Article contributed by [Sachin](https://github.com/edorado93) | ||
|
||
|
||
|
||
|
60 changes: 60 additions & 0 deletions
60
Graphs-And-Trees/Largest-Value-in-Each-Tree-Row/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
class Solution { | ||
public List<Integer> largestValues(TreeNode root) { | ||
|
||
// Base case where the tree is empty | ||
if (root == null) { | ||
return new LinkedList<Integer>(); | ||
} | ||
|
||
// Initialize the queue with the root node | ||
// This is the level-0 | ||
Queue<TreeNode> Q = new LinkedList<TreeNode>(); | ||
Q.add(root); | ||
|
||
List<Integer> maxPerLevel = new LinkedList<Integer>(); | ||
int levelMax = -1; | ||
TreeNode currentNode; | ||
|
||
// Iterate until the queue becomes empty | ||
while (Q.size() > 0) { | ||
|
||
// Number of elements in the current level | ||
int numElementsCurrentLevel = Q.size(); | ||
|
||
// Resetting the level maximum to the first node's value in the current level | ||
levelMax = Q.peek().val; | ||
|
||
// Iterate over all the elements in the current level | ||
for (int i = 0; i < numElementsCurrentLevel; i++) { | ||
currentNode = Q.remove(); | ||
|
||
// Update the level maximum | ||
levelMax = Math.max(levelMax, currentNode.val); | ||
|
||
// Push the left child into the queue if one exists | ||
if (currentNode.left != null) { | ||
Q.add(currentNode.left); | ||
} | ||
|
||
// Push the right child into the queue if one exists | ||
if (currentNode.right != null) { | ||
Q.add(currentNode.right); | ||
} | ||
} | ||
|
||
// Append the maximum element to the array we have to return | ||
maxPerLevel.add(levelMax); | ||
} | ||
|
||
return maxPerLevel; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Graphs-And-Trees/Largest-Value-in-Each-Tree-Row/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution: | ||
def largestValues(self, root: TreeNode) -> List[int]: | ||
|
||
# Base case where the tree is empty | ||
if not root: | ||
return [] | ||
|
||
# Initialize the queue with the root node | ||
# This is the level-0 | ||
Q = [root] | ||
max_per_level = [] | ||
|
||
# Iterate until the queue becomes empty | ||
while Q: | ||
|
||
# Number of elements in the current level | ||
num_elements_current_level = len(Q) | ||
|
||
# Resetting the level maximum to the first node's value in the current level | ||
level_max = Q[0].val | ||
|
||
# Iterate over all the elements in the current level | ||
for _ in range(num_elements_current_level): | ||
element = Q.pop(0) | ||
|
||
# Update the level maximum | ||
level_max = max(level_max, element.val) | ||
|
||
# Push the left child into the queue if one exists | ||
if element.left: | ||
Q.append(element.left) | ||
|
||
# Push the right child into the queue if one exists | ||
if element.right: | ||
Q.append(element.right) | ||
|
||
# Append the maximum element to the array we have to return | ||
max_per_level.append(level_max) | ||
return max_per_level |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters