-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleftViewOfBinaryTree
53 lines (39 loc) · 1.17 KB
/
leftViewOfBinaryTree
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
52
53
/************************************************************
Following is the TreeNode class structure
class TreeNode<T>
{
public:
T data;
TreeNode<T> left;
TreeNode<T> right;
TreeNode(T data)
{
this.data = data;
left = null;
right = null;
}
};
************************************************************/
import java.util.ArrayList;
public class Solution
{
static int maxLeft = 0;
public static ArrayList<Integer> getLeftView(TreeNode<Integer> root)
{
maxLeft = 0;
ArrayList<Integer> al = new ArrayList<>();
getLeftView(root, al, 1);
return al;
}
public static void getLeftView(TreeNode<Integer> root, ArrayList<Integer> al, int left){
if(root == null){
return;
}
if(left > maxLeft){
al.add(root.data);
maxLeft = Math.max(maxLeft, left);
}
getLeftView(root.left, al, left + 1);
getLeftView(root.right, al, left + 1);
}
}