Ejemplo n.º 1
0
  private static void getSum(
      int totalSum, int curVal, IntNode root, List<IntNode> path, Set<List<IntNode>> paths) {
    if (root == null) {
      if (totalSum == curVal) {
        paths.add(path);
      } else {
        int subSum = 0;
        for (int i = path.size() - 1; i >= 0; i--) {
          IntNode node = path.get(i);
          subSum += node.getValue();
          if (subSum == totalSum) {
            List<IntNode> newPath = path.subList(i, path.size());
            paths.add(newPath);
            //                        break;//keep if you only want shortest list
          }
        }
      }
      return;
    }

    //        System.out.println("Node:"+root.getValue()+" Sum:"+newSum);
    path.add(root);
    ArrayList<IntNode> pathCopy = Lists.newArrayList(path);
    getSum(totalSum, curVal, root.getLeftNode(), path, paths);
    getSum(totalSum, curVal, root.getRightNode(), pathCopy, paths);
  }
Ejemplo n.º 2
0
 private static void printPath(Set<List<IntNode>> paths) {
   for (List<IntNode> path : paths) {
     for (IntNode node : path) {
       System.out.print(node.getValue() + ",");
     }
     System.out.println();
   }
 }
Ejemplo n.º 3
0
  public void testAsBoolean() throws Exception {
    assertEquals(false, BooleanNode.FALSE.asBoolean());
    assertEquals(true, BooleanNode.TRUE.asBoolean());
    assertEquals(false, IntNode.valueOf(0).asBoolean());
    assertEquals(true, IntNode.valueOf(1).asBoolean());
    assertEquals(false, LongNode.valueOf(0).asBoolean());
    assertEquals(true, LongNode.valueOf(-34L).asBoolean());
    assertEquals(true, new TextNode("true").asBoolean());
    assertEquals(false, new TextNode("false").asBoolean());
    assertEquals(false, new TextNode("barf").asBoolean());
    assertEquals(true, new TextNode("barf").asBoolean(true));

    assertEquals(true, new POJONode(Boolean.TRUE).asBoolean());
  }
Ejemplo n.º 4
0
 public void testAsInt() throws Exception {
   assertEquals(9, IntNode.valueOf(9).asInt());
   assertEquals(7, LongNode.valueOf(7L).asInt());
   assertEquals(13, new TextNode("13").asInt());
   assertEquals(0, new TextNode("foobar").asInt());
   assertEquals(27, new TextNode("foobar").asInt(27));
   assertEquals(1, BooleanNode.TRUE.asInt());
 }
Ejemplo n.º 5
0
  public static void main(String[] args) {
    IntNode d = new IntNode(5);
    d.setLeftNode(new IntNode(2));
    d.setRightNode(new IntNode(4));

    IntNode e = new IntNode(7);
    e.setLeftNode(new IntNode(-2));

    IntNode f = new IntNode(9);
    IntNode g = new IntNode(10);

    IntNode b = new IntNode(-6);
    b.setLeftNode(d);
    b.setRightNode(e);

    IntNode c = new IntNode(-3);
    c.setLeftNode(f);
    c.setRightNode(g);

    IntNode a = new IntNode(1);
    a.setLeftNode(b);
    a.setRightNode(c);

    printSums(a, 2);
  }