解法一:递归

int countNodes(TreeNode* root)
{
if (root == NULL)
return ; TreeNode *pLeft = root->left;
TreeNode *pRight = root->right;
int ldepth = , rdepth = ;
while (pLeft) {
ldepth++;
pLeft = pLeft->left;
}
while (pRight) {
rdepth++;
pRight = pRight->right;
}
if (ldepth == rdepth)
return ( << (ldepth + )) - ;
else
return countNodes(root->left) + countNodes(root->right) + ;
}

解法二:迭代

int GetDepth(TreeNode *root)
{
int depth = ;
while (root) {
depth++;
root = root->left;
}
return depth;
} int countNodes(TreeNode* root)
{
if (root == NULL)
return ; int depth = GetDepth(root);
int leaf = ;
int depth_left, depth_right;
while (true) {
depth_left = GetDepth(root->left);
depth_right = GetDepth(root->right);
if (depth_left == && depth_right == ) {
leaf += ;
break;
} if (depth_left == depth_right) {
leaf += << (depth_left - );
root = root->right;
} else {
root = root->left;
}
}
return ( << (depth - )) - + leaf;
}

【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes的更多相关文章

  1. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  2. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

  3. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  4. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  5. 完全二叉树的节点个数 Count Complete Tree Nodes

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  6. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  9. LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

随机推荐

  1. 关于Controller层返回JSON字符串

    /** * 导入jackson包. * @param pn * @return */ @RequestMapping("/emps") @ResponseBody public M ...

  2. 使用 Nginx 提升网站访问速度

    使用 Nginx 提升网站访问速度 http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/ Nginx 简介 Nginx ("engine ...

  3. C++模拟Http/Https访问web站点

    一.概述 1.Http与Https的区别与联系 在OSI参考模型中Http与Https均属于应用层协议.Http即Hypertext Transfer Protocol,超文本传输协议:而Https为 ...

  4. 4.windows如何导入python包

    python链接:https://www.python.org/downloads/release/python-2715/ pip链接:https://pypi.org/project/pip/#f ...

  5. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  6. Linux安装rpm包时报错Header V3 DSA/SHA1 Signature, key ID 1d1e034b: NOKEY解决办法

    这是因为yum安装了旧版本的GPG key造成的,解决办法: rpm --import /etc/pki/rpm-gpg/RPM* Header V3 DSA/SHA1 Signature, key ...

  7. MySQL5.7多源复制

    MySQL5.7开始支持多源复制,也就是多主一从的复制架构: 使用多源复制的考虑: 1.灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了),还有最后一个救命稻草: 2.备份:直接 ...

  8. 协变返回类型---《C++必知必会》 条款 31

    一般来说,一个重写的函数与被它重写的函数具有相同的返回类型. 然而,这个规则对于“协变返回类型(covariant  return type)“的情形来说有所放松.也就是说,如果B是一个类类型,并且一 ...

  9. 文件下载—SSH框架文件下载

    1.准备下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>commons- ...

  10. Core Java 6

    p277~p279: 1.使用解耦合的 try/catch 和 try/finally 语句块可以提高代码的清晰度,并且会报告 finally 子句中出现的错误. 2.假设利用 return 语句从 ...