-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftView.java
More file actions
31 lines (29 loc) · 916 Bytes
/
LeftView.java
File metadata and controls
31 lines (29 loc) · 916 Bytes
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
package BinaryTree;
import java.util.*;
public class LeftView {
static ArrayList<TreeNode> ans = new ArrayList<>();
public static void main(String args[]) {
TreeNode root = new TreeNode(1);
root . left = new TreeNode(3);
root . left . left = new TreeNode(5);
root . left . left . left = new TreeNode(7);
root . right = new TreeNode(2);
root . right . right = new TreeNode(4);
root . right . right . right = new TreeNode(6);
leftView(root,0);
for(TreeNode node:ans)
{
System.out.print(node.data + " ");
}
}
public static void leftView(TreeNode root,int level)
{
if(root == null)return;
if(ans.size() == level)
{
ans.add(root);
}
leftView(root.left , level + 1);
leftView(root.right , level +1);
}
}