# Sum Single Childrenby Isai Damier, Android Engineer @ Google

```/**************************************************************************
* Author: Isai Damier
* Title: Sum Single Children of BST
* Project: geekviewpoint
* Package: algorithms
*
* Time Complexity of Solution:
*   Best = Average = Worst = O(n).
*
* Description: Not every node in a BST has two children. A node
*    may have no children at all (e.g. a leaf) or a node may
*    have a single child. This function aggregates the values
*    of all nodes that are single children. If the tree is null,
*    the function returns -1.
*
* Technical Details: In this implementation, the root of the
*     tree is counted as a single child.
*
**************************************************************************/
public int sumSingleChildren() {
if (null == root) {
return -1;
}
Node n;
int sum = root.data;//the root is a single child
while (!queue.isEmpty()) {
n = queue.remove();//dequeue
if (null != n.left && null != n.right) {//nothing to add
} else if (null != n.left) {
sum += n.left.data;
} else if (null != n.right) {
sum += n.right.data;
}//if-else if-else if
}//while
return sum;
}```
```import org.junit.Test;
import static org.junit.Assert.*;

public class BSTTest {

/**
* Test of sumSingleChildren method, of class BST.
*/
@Test
public void testSumSingleChildren() {
System.out.println(""sumSingleChildren"");
BST bst = new BST();
int[] treeTape = {200, 100, 300, 50, 150, 250, 350, 25, 75, 125,
175, 225, 275, 325, 375, 35, 212, 312, 400};
assertEquals(-1, bst.sumSingleChildren());
//set expectation
int expected = 200 + 35 + 212 + 312 + 400;//draw the tree on paper