-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftViewBinaryTree.java
More file actions
48 lines (44 loc) · 1.33 KB
/
LeftViewBinaryTree.java
File metadata and controls
48 lines (44 loc) · 1.33 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class LeftViewBinaryTree {
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.right.left = new Node(4);
root.right.left.right = new Node(5);
LeftViewBinaryTree lbt = new LeftViewBinaryTree();
ArrayList<Integer> l = lbt.leftView(root);
l.forEach(i -> System.out.println(i));
}
public ArrayList<Integer> leftView(Node root) {
Queue<NodeEntry> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
if (root==null) return list;
queue.add(new NodeEntry(root, 0));
while (!queue.isEmpty()) {
NodeEntry entry = queue.poll();
if (entry.node.left!=null) queue.offer(new NodeEntry(entry.node.left, entry.level+1));
if (entry.node.right!=null) queue.offer(new NodeEntry(entry.node.right, entry.level+1));
if (list.size()==entry.level) list.add(entry.node.data);
}
return list;
}
}
class Node {
int data;
Node left, right;
Node(int val) {
this.data = val;
this.left = null;
this.right = null;
}
}
class NodeEntry {
public Node node;
public int level;
public NodeEntry(Node node, int level) {
this.node = node; this.level = level;
}
}