LeetCode 222. Count Complete Tree Nodes 题解——Java
2017-03-15 09:14
375 查看
题目链接:https://leetcode.com/problems/count-complete-tree-nodes/#/description
题目要求:计算完全二叉树的节点个数
思路:首先想到的是直接的递归,二叉树的节点个数 = 左子树的节点个数 + 右子树的节点个数 + 1
Java 代码如下:
public class Solution {
// 二叉树的节点数 = 左子树的节点数 + 右子树的节点数 + 1
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
遍历了整个二叉树,时间复杂度为O(N)。然后超时了,因为上述方法并没有使用“完全二叉树”这个条件。
接下来考虑,对于完全二叉树,其左子树和右子树中至少有一个子树是满二叉树,而满二叉树的节点个数可以直接由 2^n-1得到,因此,是满二叉树的那一部分就不需要再遍历,因此可以提高效率。算法思路如下:首先计算出二叉树的最左侧分支和最右侧分支的层数,如果二者相等,则整个二叉树是满二叉树;若不相等,则递归的计算左右子树的节点数,总结点数=左子树节点数+右子树节点数+1。
Java代码如下:
public class Solution {
// 获取左子树的高度(其实是最左侧分支)
public int getLeftHeight(TreeNode root) {
int count = 0;
while (root != null) {
count++;
root = root.left;
}
return count;
}
// 获取右子树的高度(其实是最右侧分支的高度)
public int getRightHeight(TreeNode root) {
int count = 0;
while (root != null) {
count++;
root = root.right;
}
return count;
}
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getLeftHeight(root);
int rightHeight = getRightHeight(root);
if (leftHeight == rightHeight) {
// 表示是满二叉树,二叉树的节点数直接由公式2^n-1得到
// leftHeight即为层数, 1 << leftHeight使用位运算计算2^leftHeight,效率更高
// 注意(1 << leftHeight) - 1 的括号必须有!!
return (1 << leftHeight) - 1;
} else {
// 若该二叉树不是满二叉树,递归的调用该方法,计算左子树和右子树的节点数
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
}
题目要求:计算完全二叉树的节点个数
思路:首先想到的是直接的递归,二叉树的节点个数 = 左子树的节点个数 + 右子树的节点个数 + 1
Java 代码如下:
public class Solution {
// 二叉树的节点数 = 左子树的节点数 + 右子树的节点数 + 1
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
遍历了整个二叉树,时间复杂度为O(N)。然后超时了,因为上述方法并没有使用“完全二叉树”这个条件。
接下来考虑,对于完全二叉树,其左子树和右子树中至少有一个子树是满二叉树,而满二叉树的节点个数可以直接由 2^n-1得到,因此,是满二叉树的那一部分就不需要再遍历,因此可以提高效率。算法思路如下:首先计算出二叉树的最左侧分支和最右侧分支的层数,如果二者相等,则整个二叉树是满二叉树;若不相等,则递归的计算左右子树的节点数,总结点数=左子树节点数+右子树节点数+1。
Java代码如下:
public class Solution {
// 获取左子树的高度(其实是最左侧分支)
public int getLeftHeight(TreeNode root) {
int count = 0;
while (root != null) {
count++;
root = root.left;
}
return count;
}
// 获取右子树的高度(其实是最右侧分支的高度)
public int getRightHeight(TreeNode root) {
int count = 0;
while (root != null) {
count++;
root = root.right;
}
return count;
}
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getLeftHeight(root);
int rightHeight = getRightHeight(root);
if (leftHeight == rightHeight) {
// 表示是满二叉树,二叉树的节点数直接由公式2^n-1得到
// leftHeight即为层数, 1 << leftHeight使用位运算计算2^leftHeight,效率更高
// 注意(1 << leftHeight) - 1 的括号必须有!!
return (1 << leftHeight) - 1;
} else {
// 若该二叉树不是满二叉树,递归的调用该方法,计算左子树和右子树的节点数
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
}
相关文章推荐
- leetcode 222. Count Complete Tree Nodes
- [leetcode] 222. Count Complete Tree Nodes
- leetcode 222. Count Complete Tree Nodes
- Leetcode 222. Count Complete Tree Nodes
- 【Leetcode】222. Count Complete Tree Nodes
- 【LeetCode】222. Count Complete Tree Nodes
- leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量 + DFS深度优先遍历 + 公式计算
- [leetcode]222. Count Complete Tree Nodes
- LeetCode 222. Count Complete Tree Nodes
- Leetcode 222. Count Complete Tree Nodes
- (未完成!)Leetcode 222. Count Complete Tree Nodes
- leetcode222. Count Complete Tree Nodes
- [leetcode] 222. Count Complete Tree Nodes 解题报告
- [leetcode]222. Count Complete Tree Nodes
- LeetCode 222. Count Complete Tree Nodes
- 【LeetCode】222. Count Complete Tree Nodes-完全二叉树的结点个数
- Java for LeetCode 222 Count Complete Tree Nodes
- leetcode 222. Count Complete Tree Nodes
- LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解
- 222. Count Complete Tree Nodes 题解