Skip to content

Instantly share code, notes, and snippets.

@yokolet
Last active September 10, 2019 19:05
Show Gist options
  • Save yokolet/64781fb9082954396c34272dd94ae705 to your computer and use it in GitHub Desktop.
Save yokolet/64781fb9082954396c34272dd94ae705 to your computer and use it in GitHub Desktop.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class CBTNodeCount {
private int getHeight(TreeNode root) {
if (root == null) { return 0; }
return 1 + getHeight(root.left);
}
public int countNodes(TreeNode root) {
if (root == null) { return 0; }
int left = getHeight(root.left);
int right = getHeight(root.right);
if (left == right) {
return (int)Math.pow(2, left) + countNodes(root.right);
} else {
return (int)Math.pow(2, right) + countNodes(root.left);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment