if (node == null) { return0; } else { // to some extent, this is post-order, LRD traversal return Math.max(postorder(node.left), postorder(node.right)) + 1; }
publicintcountNodes(TreeNode root) { if (root == null) { return0; }
TreeNodeleft= root.left; intleftDepth=0; while (left != null) { left = left.left; leftDepth++; }
TreeNoderight= root.right; intrightDepth=0; while (right != null) { right = right.right; rightDepth++; }
// if it is a full binary tree, easy culculation if (leftDepth == rightDepth) { return (2 << leftDepth) - 1; }else { return countNodes(root.left) + countNodes(root.right) + 1; }