/** * Given a complete binary tree, count the number of nodes. * * Definition of a complete binary tree from Wikipedia: * * In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last * level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. * * Tags: Tree, Binary Search * * Similar Problems: (E) Closest Binary Search Tree Value */ public class CountCompleteTreeNodes { public int countNodes(TreeNode root) { if (root == null) { return 0; } int leftHeight = fullTreeHeight(root.left); int rightHeight = fullTreeHeight(root.right); if (leftHeight == rightHeight) { return (1 << leftHeight) + countNodes(root.right); } else { return (1 << rightHeight) + countNodes(root.left); } } private int fullTreeHeight(TreeNode root) { if (root == null) return 0; int res = 0; while (root != null) { root = root.left; res++; } return res; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } }