-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion222.java
More file actions
71 lines (64 loc) · 1.97 KB
/
Copy pathQuestion222.java
File metadata and controls
71 lines (64 loc) · 1.97 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
60
61
62
63
64
65
66
67
68
69
70
71
public class Question222
{
public static void move(TreeNode root, boolean[] completed, int level,int depth,int[] count)
{
if(completed[0] == false)
{
if(level==depth-1)
{
if(root.left!=null)
count[0]++;
else
completed[0] = true;
if(root.right!=null)
count[0]++;
else
completed[0] = true;
}
else
{
move(root.left,completed,level+1,depth,count);
move(root.right,completed,level+1,depth,count);
}
}
}
public static int countNodes(TreeNode root)
{
TreeNode temp = root;
int depth = 0;
while(temp.left!=null)
{
depth++;
temp = temp.left;
}
depth++;
//System.out.println(depth);
boolean[] completed = new boolean[1];
int[] count = new int[1];
move(root,completed,1,depth,count);
for(int i = 0;i<depth-1;i++)
{
count[0]+=(int)Math.pow(2,i);
}
System.out.println("Count:"+count[0]);
return count[0];
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(146,null,null);
TreeNode l1 = new TreeNode(71,null,null);
TreeNode r1 = new TreeNode(-13,null,null);
TreeNode ll2 = new TreeNode(55,null,null);
TreeNode rr2 = new TreeNode(231,null,null);
TreeNode last = new TreeNode(399,null,null);
TreeNode lastnew = new TreeNode(321,null,null);
TreeNode lastnew2 = new TreeNode(-33,null,null);
root.left = l1;
root.right = r1;
l1.left = ll2;
l1.right = last;
r1.left = rr2;
Trees.display(root);
countNodes(root);
}
}