222. Count Complete Tree Nodes
题目:
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.
链接: http://leetcode.com/problems/count-complete-tree-nodes/
题解:
求完全二叉树的节点数目。注意完全二叉树和满二叉树Full Binary Tree的唯一区别是,完全二叉树最后一层的节点不满,而且假设最后一层有节点,都是从左边开始。 这样我们可以利用这个性质得到下面两个结论:
- 假如左子树高度等于右子树高度,则右子树为完全二叉树,左子树为满二叉树。
- 假如高度不等,则左字数为完全二叉树,右子树为满二叉树。
- 求高度的时候只往左子树来找。
Time Complexity - O(logn * logn), Space Complexity - O(1) (不考虑递归栈)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; int leftHeight = getHeight(root.left); int rightHeight = getHeight(root.right); if(leftHeight == rightHeight) return (1 << leftHeight) + countNodes(root.right); else return (1 << rightHeight) + countNodes(root.left); } private int getHeight(TreeNode root) { int height = 0; while(root != null) { root = root.left; height++; } return height; } }
题外话:
高中最好哥们的父亲突然去世了,很为他感到难过,希望他节哀并且早日振作。其实在国外,最担心的就是父母身体,但受签证所限,也许一年也见不到父母一次。父母都已经年近60,算下来以后难道见面时间只有几十次??这样究竟值不值得??这种尴尬窘境,是不是等到绿卡之后才能得以解决。
二刷:
这道题有好些方法可以做。Stefan Pochmann的帖子里就写了很多。
下面我们介绍最基本最Naive的一种。
- 完全二叉树Complete Binary Tree是指除了最后一行以外,每一行都是满的。如果最后一行存在节点,那么这些节点满足从左到右排列。
- 满二叉树Full Binary Tree是指所有的节点的子节点要么为空要么有两个子节点。而满二叉树的节点总数是 2 ^ h - 1,这里h是满二叉树的高度。
- 知道了以上性质,我们就可以进行解题
- 先构造一个getHeight方法, 用来求出二叉树的高度。这里我们只用求从根节点到最左端节点的长度。
- 求出根节点左子树高度leftHeight和根节点右子树高度rightHeight
- 假如两者相等,那么说明左子树是满二叉树,而右子树可能是完全二叉树。
- 我们可以返回 2 ^ leftHeight - 1 + 1 + countNodes(root.right)
- 这里+1是因为把根节点也算进去,化简一下就是 1 << leftHeight + countNodes(root.right),返回结果
- 否则两者不等,说明左子树是完全二叉树,右子树是满二叉树
- 我们可以返回 2^ rightHeight - 1 + 1 + countNodeS(root.left)
- 化简以后得到 1 << rightHeight + countNodes(root.left),返回结果
- 假如两者相等,那么说明左子树是满二叉树,而右子树可能是完全二叉树。
- 这里getHeight()方法的时间复杂度是O(logn), countNodes()方法的时间复杂度也是O(logn),所以总的时间复杂度是O(logn * logn)
- 空间复杂度是递归栈的深度,是O(logn)
Java:
Time Complexity - O(logn * logn), Space Complexity - O(logn)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int countNodes(TreeNode root) { if (root == null) return 0; int leftHeight = getHeight(root.left); int rightHeight = getHeight(root.right); if (leftHeight == rightHeight) return (1 << leftHeight) + countNodes(root.right); else return (1 << rightHeight) + countNodes(root.left); } private int getHeight(TreeNode root) { int height = 0; while(root != null) { root = root.left; height++; } return height; } }
Reference:
https://leetcode.com/discuss/38884/ac-java-code-any-improvement
https://leetcode.com/discuss/38894/java-solution-clean-code-easy-to-understand
https://leetcode.com/discuss/38899/easy-short-c-recursive-solution
https://leetcode.com/discuss/38919/concise-java-iterative-solution-o-logn-2
https://leetcode.com/discuss/38930/concise-java-solutions-o-log-n-2
https://leetcode.com/discuss/38930/concise-java-solutions-o-log-n-2
https://leetcode.com/discuss/39462/c-solution-inspired-by-couple-of-good-ones
https://leetcode.com/discuss/45260/accepted-clean-java-solution
https://leetcode.com/discuss/57025/68ms-c-solution-using-binary-search-with-brief-explanation
https://en.wikipedia.org/wiki/Binary_tree