Skip to content

Commit

Permalink
Merge pull request #109 from DivyaGodayal/largest_val_each_row
Browse files Browse the repository at this point in the history
Added article and animation for leetcode-515
  • Loading branch information
edorado93 authored Jul 13, 2019
2 parents 2a0e723 + 9805693 commit fd6d05d
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Graphs-And-Trees/Largest-Value-in-Each-Tree-Row/README.md
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 Graphs-And-Trees/Largest-Value-in-Each-Tree-Row/solution.java
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 Graphs-And-Trees/Largest-Value-in-Each-Tree-Row/solution.py
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
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-13.png
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 added Images/Largest-Value-in-Each-Tree-Row/anim-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Largest-Value-in-Each-Tree-Row/anim-9.png
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.
Binary file added Images/Largest-Value-in-Each-Tree-Row/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,15 @@ There are multiple ways in which you can contribute. There are no "prerequisites
</tr>
<tr>
<td>13</td>
<td>Largest-Value in Each Tree Row</td>
<td>LeetCode</td>
<td><a href="https://github.com/DivyaGodayal/CoderChef-Kitchen/tree/master/Graphs-And-Trees/Largest-Value-in-Each-Tree-Row">Link</a></td>
</tr>
<tr>
<td>14</td>
<td>Construct a Binary Tree from in-order and Postorder Traversal</td>
<td>LeetCode</td>
<td><a href="https://github.com/DivyaGodayal/CoderChef-Kitchen/tree/master/Graphs-And-Trees/Binary-Tree-from-Inorder-and-Postorder-Traversal">Link</a></td>
<td><a href="https://github.com/DivyaGodayal/CoderChef-Kitchen/tree/master/Graphs-And-Trees/Binary-Tree-from-Inorder-and-Postorder-Traversal">Link</a></td>
</tr>
<tr>
<th colspan="4"><h3>Linked-List</h3></th>
Expand Down

0 comments on commit fd6d05d

Please sign in to comment.