You have to find left view of a binary tree.
input : tree root
output : left view of a tree
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
package com.tachyon.basic;
public class TreeProblems {
static int max = 0;
public static void printLeftView(TreeNode root) {
print(root, 1);
}
public static void print(TreeNode root, int level) {
if (root == null)
return;
if (max < level) {
System.out.print(root.data + " ");
max = level;
}
print(root.left, level + 1);
print(root.right, level + 1);
}
public static void main(String[] arg) {
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.right.left = new TreeNode(13);
printLeftView(root);
}
}
output : 10 5 13
Comments