forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeSumOfValuesAtOddHeight.java
60 lines (53 loc) · 1.07 KB
/
BinaryTreeSumOfValuesAtOddHeight.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
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/03/binary-tree-sum-of-nodes-at-odd-levels.html
*/
package com.dsalgo;
public class BinaryTreeSumOfValuesAtOddHeight
{
public static void main(String[] args)
{
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
Node d = new Node(4);
Node e = new Node(5);
Node f = new Node(8);
Node g = new Node(6);
Node h = new Node(9);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;
f.left = g;
f.right = h;
int sum = findOddLevelSum(a);
System.out.println(sum);
}
private static int findOddLevelSum(Node a)
{
return findOddLevelSum(a, true);
}
private static int findOddLevelSum(Node a, boolean oddLevel)
{
if (a == null)
return 0;
int childSum=findOddLevelSum(a.left, !oddLevel)
+ findOddLevelSum(a.right, !oddLevel);
if (oddLevel)
return a.value + childSum;
else
return childSum;
}
static class Node
{
Node left;
Node right;
int value;
public Node(int value)
{
this.value = value;
}
}
}