Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 8
- /
- 3 2
- /
- 1 2
- public boolean isValid()
- {
- return isValid(root);
- }
- public boolean isValid(BinaryNode<E> node)
- {
- if (node == null)
- return true;
- int sum = sumOfSub(node.getLeft()) + sumOfSub(node.getRight());
- if (node.getLeft() != null && node.getRight() != null)
- {
- if ((Integer) node.getData() != sum)
- {
- return false;
- }
- }
- else
- return true;
- return isValid(node.getLeft()) && isValid(node.getRight());
- }
- private int sumOfSub(BinaryNode node)
- {
- if (node == null) return 0;
- int value = (Integer) node.getData();
- return value + sumOfSub(node.getLeft()) + sumOfSub(node.getRight());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement