Count Complete Tree Nodes 统计完全二叉树的节点数
来源:互联网 发布:肥皂水灌肠的浓度算法 编辑:程序博客网 时间:2024/06/02 18:54
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.
/** * 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) { return solve(root); } int solve(TreeNode *root) { if(root == NULL) return 0; int left=solve(root->left); int right=solve(root->right); return left+right+1; } */ int countNodes(TreeNode* root) { if(root==NULL) return 0; int numL=0,numR=0; TreeNode *lt=root; TreeNode *rt=root; while(lt!=NULL) { numL++; lt=lt->left; } while(rt!=NULL) { numR++; rt=rt->right; } if(numL==numR) return pow(2,numL)-1; else return countNodes(root->left)+countNodes(root->right)+1; }};
0 0
- Count Complete Tree Nodes 统计完全二叉树的节点数
- LeetCode 222. Count Complete Tree Nodes(完全二叉树节点数统计)
- Tree-----222. Count Complete Tree Nodes(统计完全二叉树的节点个数)
- 222. Count Complete Tree Nodes | 完全二叉树的节点数
- 222. Count Complete Tree Nodes 完全二叉树节点
- leetcode 222 Count Complete Tree Nodes (计算完全二叉树节点数)
- leetcode222---Count Complete Tree Nodes(求完全二叉树节点数)
- Leetcode 222 Count Complete Tree Nodes 完全二叉树的结点数
- Count Complete Tree Nodes (求完全二叉树节点树)
- 222.leetcode.Count Complete Tree Nodes(medium)[完全二叉树 节点个数]
- leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量
- LeetCode OJ 之 Count Complete Tree Nodes(计算完全二叉树的结点数目)
- LeetCode Count Complete Tree Nodes 二叉树
- Count Complete Tree Node 计算完全二叉树的节点个数。
- LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解
- 统计完全二叉树的节点数
- 统计完全二叉树的节点数
- Count Complete Tree Nodes
- mysql免安装版配置
- iOS-项目开发,细节,报错,处理(2)
- ssh2. 相关配置文件
- 赫夫曼编译码器实验报告
- Maximal Square 最大的正方形
- Count Complete Tree Nodes 统计完全二叉树的节点数
- ThumbnailUtils源码
- 实验报告图遍历的演示
- 2015-06-12
- 贝叶斯网络
- Prim最小生成树算法
- linux命令9--diffstat
- JS完美运动框架
- 马尔科夫网络