LeetCode OJ 之 Count Complete Tree Nodes(计算完全二叉树的结点数目)
来源:互联网 发布:tortoisesvn for mac 编辑:程序博客网 时间:2024/04/20 20:33
题目:
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.
思路:
最简单的思路:return 1 + countNodes(root->left) + countNodes(root->right);
可惜超时了。由于是完全二叉树,根结点的左右子树至少有一个是满二叉树,因此可以在递归之前先算出满二叉树的结点数目。
代码1:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int countNodes(TreeNode *root) { if(root == NULL) return 0; int height = 0; TreeNode *leftNode = root , *rightNode = root; while(leftNode && rightNode) { leftNode = leftNode->left; rightNode = rightNode->right; height++; } //如果leftNode为空,说明是满二叉树,返回2^h-1 if(leftNode == NULL) return (1 << height) - 1;//这里一定要加(),因为-的优先级高于移位 else return 1 + countNodes(root->left) + countNodes(root->right); } };
代码2:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int height(TreeNode *root) { if(root == NULL) return -1; //叶结点的子节点返回-1,这把叶结点那一行抵消掉了,故计算的高度不包括最后一行 else return 1 + height(root->left); } int countNodes(TreeNode *root) { int h = height(root); if(h < 0) return 0; if(height(root->right) == h-1) return (1 << h) + countNodes(root->right);//如果root->right返回h-1,则说明最后一行的最后一个结点位于右子树,即说明左子树是满二叉树,左子树的结点数目即为2^h-1,再加上根结点就是2^h,然后+右子树的结点数 else return (1 << h-1) + countNodes(root->left);//如果root->right返回的<h-1,则说明最后一行的最后一个结点位于左子树,即说明右子树的满二叉树,右子树的结点数目即为2^(h-1)-1,再加上根结点就是2^(h-1),然后+左子树的结点数 }};
代码3:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int height(TreeNode *root) { if(root == NULL) return -1; //叶结点的子节点返回-1,这把叶结点那一行抵消掉了,故计算的高度不包括最后一行 else return 1 + height(root->left); } int countNodes(TreeNode *root) { int result = 0; int h = height(root); if(h < 0) return 0; while(root) { if(height(root->right) == h-1) { result += 1<<h; root = root->right; } else { result += 1<<h-1; root = root->left; } h--; } return result; }};
0 0
- LeetCode OJ 之 Count Complete Tree Nodes(计算完全二叉树的结点数目)
- Leetcode 222 Count Complete Tree Nodes 完全二叉树的结点数
- leetcode 222 Count Complete Tree Nodes (计算完全二叉树节点数)
- LeetCode Count Complete Tree Nodes 二叉树
- LeetCode 222. Count Complete Tree Nodes(完全二叉树节点数统计)
- leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量
- 222. Count Complete Tree Nodes 完全二叉树节点
- 222.leetcode.Count Complete Tree Nodes(medium)[完全二叉树 节点个数]
- Tree-----222. Count Complete Tree Nodes(统计完全二叉树的节点个数)
- Count Complete Tree Nodes 统计完全二叉树的节点数
- 222. Count Complete Tree Nodes | 完全二叉树的节点数
- Count Complete Tree Nodes (求完全二叉树节点树)
- [LeetCode] Count Complete Tree Nodes
- leetcode - Count Complete Tree Nodes
- Leetcode: Count Complete Tree Nodes
- [LeetCode]Count Complete Tree Nodes
- LeetCode Count Complete Tree Nodes
- [leetcode] Count Complete Tree Nodes
- 网狐6878完整大厅源码+完美服务端组件+金蟾捕鱼(完美库存)
- 【进阶android】ListView源码分析——总述
- XML应用程序开发--上
- OpenFlow Switch Specification 1.3.0 (一)
- Linux_1.4_应用程序地址布局
- LeetCode OJ 之 Count Complete Tree Nodes(计算完全二叉树的结点数目)
- 如何创建并运行java线程
- 关于undefined referenced xxxx的另一种原因
- Eclipse格式化换行宽度设置
- 用 Java 实现断点续传 (HTTP)
- SAT数学考试备考攻略
- linux系统添加根证书 linux证书信任列表
- 九、网络编程
- 各国虚拟主机大盘点: