-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathBTLEFTVIEW
More file actions
59 lines (51 loc) · 1.17 KB
/
BTLEFTVIEW
File metadata and controls
59 lines (51 loc) · 1.17 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
52
53
54
55
56
57
58
59
node and key value*/
class Node {
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
/* Class to print the left view */
class BinaryTree {
Node root;
static int max_level = 0;
// recursive function to print left view
void leftViewUtil(Node node, int level)
{
// Base Case
if (node == null)
return;
// If this is the first node of its level
if (max_level < level) {
System.out.print(node.data + " ");
max_level = level;
}
// Recur for left and right subtrees
leftViewUtil(node.left, level + 1);
leftViewUtil(node.right, level + 1);
}
// A wrapper over leftViewUtil()
void leftView()
{
max_level = 0;
leftViewUtil(root, 1);
}
/* testing for example nodes */
public static void main(String args[])
{
/* creating a binary tree and entering the nodes */
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(7);
tree.root.left.right = new Node(8);
tree.root.right.right = new Node(15);
tree.root.right.left = new Node(12);
tree.root.right.right.left = new Node(14);
tree.leftView();
}
}