Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. 8
  2. /
  3. 3 2
  4. /
  5. 1 2
  6.  
  7. public boolean isValid()
  8. {
  9. return isValid(root);
  10. }
  11.  
  12. public boolean isValid(BinaryNode<E> node)
  13. {
  14. if (node == null)
  15. return true;
  16.  
  17. int sum = sumOfSub(node.getLeft()) + sumOfSub(node.getRight());
  18.  
  19. if (node.getLeft() != null && node.getRight() != null)
  20. {
  21. if ((Integer) node.getData() != sum)
  22. {
  23. return false;
  24. }
  25. }
  26. else
  27. return true;
  28.  
  29. return isValid(node.getLeft()) && isValid(node.getRight());
  30. }
  31.  
  32. private int sumOfSub(BinaryNode node)
  33. {
  34. if (node == null) return 0;
  35. int value = (Integer) node.getData();
  36. return value + sumOfSub(node.getLeft()) + sumOfSub(node.getRight());
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement