-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_Of_Non_Leaf_In_Binary_Tree.java
74 lines (61 loc) · 1.4 KB
/
No_Of_Non_Leaf_In_Binary_Tree.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Number Of Non Leaves in a Binary Tree
package Trees;
class Node
{
int data;
Node left;
Node right;
public Node(int d)
{
data=d;
left=null;
right=null;
}
}
class NoOfNonLeaves
{
int nonleave(Node root)
{
if(root==null)
{
return 0;
}
else if(root.left==null && root.right==null)
{
return 0;
}
else
{
return (1+(nonleave(root.left)+nonleave(root.right)));
}
}
public static void main(String...args)
{
Node first=new Node(10);
Node second=new Node(20);
Node third=new Node(30);
Node fourth=new Node(40);
Node fifth=new Node(50);
Node sixth=new Node(60);
Node seventh=new Node(70);
Node eight=new Node(80);
first.left=second;
first.right=third;
second.left=fourth;
second.right=null;
third.left=fifth;
third.right=null;
fourth.left=null;
fourth.right=sixth;
fifth.left=seventh;
fifth.right=eight;
sixth.left=null;
sixth.right=null;
seventh.left=null;
seventh.right=null;
eight.left=null;
eight.right=null;
int leaves= new NoOfNonLeaves().nonleave(first);
System.out.println("No of Non Leaves in Given Binary Tree is "+leaves);
}
}