-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
51 lines (46 loc) · 1.26 KB
/
BinaryTree.java
File metadata and controls
51 lines (46 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class BinaryTree{
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
this.val = val;
}
}
public List<Integer> rightSideView(TreeNode root) {
if(root == null) return new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
ArrayList<Integer> arr = new ArrayList<>();
q.add(root);
q.add(null);
TreeNode prev=null;
while(!q.isEmpty()){
TreeNode curr = q.remove();
if(curr == null){
arr.add(prev.val);
if(q.isEmpty()){
break;
}
q.add(null);
continue;
}
if(curr.left != null){
q.add(curr.left);
}
if(curr.right != null){
q.add(curr.right);
}
prev = curr;
}
return arr;
}
//TOWER OF HANOI
public int towerOfHanoi(int n, int from, int to, int aux) {
if(n == 1 || n==0) return n;
int a = 1+towerOfHanoi(n-1, from, aux, to);
return a + towerOfHanoi(n-1, aux, to, from);
}
public static void main(String[] args) {
}
}