Skip to content

Commit 4d435dd

Browse files
committed
Day2
1 parent d1d6a54 commit 4d435dd

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Problem Statement:
2+
Source: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
3+
4+
Solution 1:
5+
6+
Simply first find the Height of the tree.
7+
For each level -> (height to 1) find the nodes which are present at that level and add that in the list.
8+
Add each list of levels to desired allLevels List. It will be desired solution.
9+
10+
Time Complexity O(n*2)
11+
Space Complexity O(n)
12+
13+
14+
I feel this solution is necessary to see, as it will teach you basic tree operations. How to calculate height, and overall get the feel of moving into binary tree in recursion.
15+
16+
17+
Solution 2:
18+
19+
Statement is to find the Level Order Traversal but from bottom up.
20+
Do Level Order Traversal level by level and save each nodes of particular level in an Array. Finally when moving to other level, save this level to
21+
allLevel Array.
22+
23+
How to Do this?
24+
25+
1. Use queue to travel level by travel. Take a Queue and add it in queue.
26+
2. Now for every loop we will have nodes for that level. (Run the loop till that level via fetching size)
27+
3. Create an ArrayList for holding that level. Get the size of current queue and loop it until size > 0
28+
4. Take element one by one and add their children if exist to the queue.
29+
5. Add that node into that ArrayList which you have created for holding that level.
30+
6. Add this level array to AllLevels Arraylist which you have created.
31+
32+
33+
Now, you have Array of Array of all levels, from top to bottom order. Reverse the collection to get the desired result.
34+
35+
Time Complexity O(n)
36+
Space Complexity O(n)
37+
38+
/* Happy Coding */

src/Day2/Solution1.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Day2;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Date 02/07/2020
9+
* @author Aman Shivhare
10+
*
11+
* Source: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
12+
*
13+
*/
14+
15+
public class Solution1 {
16+
17+
private static int height(TreeNode root){
18+
if(root==null){
19+
return 0;
20+
}
21+
return Math.max(height(root.left), height(root.right)) + 1;
22+
}
23+
24+
private static void givenLevel(TreeNode node, int level, List<Integer> levelNodes){
25+
if(node == null){
26+
return;
27+
}
28+
if(level == 1){
29+
levelNodes.add(node.val);
30+
return;
31+
}
32+
33+
givenLevel(node.left, level-1, levelNodes);
34+
givenLevel(node.right, level-1, levelNodes);
35+
}
36+
37+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
38+
int height = height(root);
39+
List<List<Integer>> allLevels = new ArrayList<>();
40+
41+
for(int i = height; i>0; i--){
42+
List<Integer> currentLevelNodes = new ArrayList<>();
43+
givenLevel(root, i, currentLevelNodes);
44+
allLevels.add(currentLevelNodes);
45+
}
46+
return allLevels;
47+
}
48+
}

src/Day2/Solution2.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Day2;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Queue;
8+
9+
/**
10+
* Date 02/07/2020
11+
* @author Aman Shivhare
12+
*
13+
* Source: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
14+
*
15+
*/
16+
17+
18+
public class Solution2 {
19+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
20+
21+
List<List<Integer>> allLevels = new ArrayList<>();
22+
Queue<TreeNode> pipe = new LinkedList<>();
23+
if(root!=null) pipe.add(root);
24+
while(!pipe.isEmpty()){
25+
int size = pipe.size();
26+
List<Integer> level = new ArrayList<>();
27+
while(size!=0){
28+
TreeNode node = pipe.poll();
29+
if(node.left != null) pipe.add(node.left);
30+
if(node.right != null) pipe.add(node.right);
31+
level.add(node.val);
32+
size--;
33+
}
34+
allLevels.add(level);
35+
}
36+
Collections.reverse(allLevels);
37+
return allLevels;
38+
}
39+
}

src/Day2/TreeNode.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Day2;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
TreeNode() {}
8+
TreeNode(int val) { this.val = val; }
9+
TreeNode(int val, TreeNode left, TreeNode right) {
10+
this.val = val;
11+
this.left = left;
12+
this.right = right;
13+
}
14+
}

0 commit comments

Comments
 (0)