Version #2 BFS[TODO]
Version #1 DFS
98.36
class Solution {
int count;
public boolean isCompleteTree(TreeNode root) {
count = getCount(root);
return validate(root, 1);
}
private boolean validate(TreeNode node, int id) {
if (node == null) return true;
if (id > count) return false;
return validate(node.left, id * 2) && validate(node.right, id * 2 + 1);
}
private int getCount(TreeNode node) {
if (node == null) return 0;
return 1 + getCount(node.left) + getCount(node.right);
}
}
No comments:
Post a Comment