Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 10, 2017 02:44
Show Gist options
  • Select an option

  • Save cixuuz/a2af4bce4cae3808019e4b823c492597 to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/a2af4bce4cae3808019e4b823c492597 to your computer and use it in GitHub Desktop.
[222. Count Complete Tree Nodes] #lc0222
public class Solution {
public int countNodes(TreeNode root) {
if (root == null)
return 0;
int lh = height(root.left);
int rh = height(root.right);
if (lh == rh) return (1 << lh) + countNodes(root.right);
else return (1 << rh) + countNodes(root.left);
}
private int height(TreeNode node) {
if (node == null) return 0;
return 1 + height(node.left);
}
}222. Count Complete Tree Nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment