https://leetcode.com/problems/count-complete-tree-nodes/

宽度优先搜索方法,超时!!

/*** 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) {int height=1;if(root==NULL)return 0;TreeNode * temp=root;stack<TreeNode *> s1;while(temp!=NULL){height++;s1.push(temp);temp=temp->right;}int count=(int)(pow(2,height)-1);bool flag=0;int len=1;int leftVal=0;int rightVal=0;while(!s1.empty()){len++;TreeNode * temp=s1.top();s1.pop();if(right==0)rightVal=getCount(temp->right);elserightVal=leftVal+right+1;leftVal=getCount(temp->left);if(leftVal!=rightVal)break;}int lack=(int)(pow(2,len)-1-(leftVal+rightVal+1));int res=count-lack;return res;}int getCount(TreeNode * node){if(node==NULL)return 0;int res=0;queue<TreeNode *> q;q.push(node);while(!q.empty()){res++;TreeNode * temp=q.front();q.pop();if(temp->left!=NULL)q.push(temp->left);if(temp->right!=NULL)q.push(temp->right);}return res;}
};

转载于:https://www.cnblogs.com/aguai1992/p/4780452.html

Count Complete Tree Nodes相关推荐

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

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

  2. 222. Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  3. Middle-题目95:222. Count Complete Tree Nodes

    题目原文: Given a complete binary tree, count the number of nodes. 题目大意: 给出一个完全二叉树,求节点数. 题目分析: 如果直接递归数节点 ...

  4. leetcode 222 Count Complete Tree Nodes

    今天做了一道leetcode题,刚开始是暴力破解,发现special judge, very large tree 过不去,然后一顿想,一顿查,发现可以运用complete binary tree 的 ...

  5. 222 Count Complete Tree Nodes

    1,这道题如果纯用递归数点而不利用其为一个complete binary tree的话会超时. 2.为了利用这个条件,比较左右两子数的高度:1, 如果相等则左子树为完全二叉树 2, 如果不等, 则右子 ...

  6. LeetCode Count Complete Tree Nodes(二分法)

    问题:给出一个完全二叉树,求其结点个数 思路:第一种方式时直接使用递归法,将其左子树个数加上右子树个数再加上根结点 第二种方式二分法,因为完全二叉树除了最后一层外,其它都满足有2^i个结点,而最后一层 ...

  7. LeetCode 222. Count Complete Tree Nodes 题解——Java

    题目链接:https://leetcode.com/problems/count-complete-tree-nodes/#/description 题目要求:计算完全二叉树的节点个数 思路:首先想到 ...

  8. leetcode 222. Count Complete Tree Nodes | 222. 完全二叉树的节点个数(Java)

    题目 https://leetcode.com/problems/count-complete-tree-nodes/ 题解 思路参考左程云<程序员代码面试指南> 顺便贴一下草稿 代码 c ...

  9. LeetCode题解-222-Count Complete Tree Nodes

    没有做出来,所以参考了https://segmentfault.com/a/1190000003818177 迭代法的图解如下:

最新文章

  1. Microsoft 用户体验虚拟化 UE-V 1.0 RC 发布
  2. python中add函数_如何使用python中的add函数?
  3. 测试用例实例--常见功能测试点(转)
  4. 【转载】async await 的前世今生(Updated)
  5. bzoj 3679: 数字之积
  6. ubuntu 16.0 CUDA 10.1卸载 CUDA 10.2 安装
  7. 清理linux清理垃圾文件夹,让Ubuntu系统释放空间最有效的五种方法(清除不需要的或垃圾文件)...
  8. win10固态硬盘分区 整数_固态硬盘怎么重装win10系统?Win10固态硬盘重装系统教程...
  9. matlab调用com组件出现错误求解决,matlab在生成COM组件编译时出现错误
  10. 数学建模【系统评价决策模型(概论、案例分析-汽车选购、层次分析法、案例分析-职员晋升、动态加权综合评价法、案例分析-大气污染问题)】
  11. DocumentBuilderFactory.newInstance() 异常解决
  12. 水晶报表导出PDF or Excel,并返回文件到用户的IE浏览器中
  13. 商业研究(10):比呀比-海淘电商-海外正品直邮-高品质跨境购物平台
  14. HECATE G2耳机麦克风问题解决
  15. 学习单片机的几点经验之谈
  16. PERT 活动图 关键路径
  17. Java中的过滤器doFilter里的chain.doFilter()函数理解
  18. 亭江鹰猫山探险---“比野猪还野猪了”
  19. 只有5行的算法——Floyd算法
  20. 用python给excel文件的第三列添加重复递增数字

热门文章

  1. nx600打印机打印设置_win7打印机共享怎么设置
  2. 用python恢复删除的文件_使用python删除N天前的文件
  3. JZOJ 5703. 【gdoi2018 day2】木板(board)
  4. 写一个php发通知公告,从我的服务器PHP发送推送通知
  5. bn层初始化参数_神经网络参数初始化方式
  6. js符号输入不可用_js禁止输入特殊字符
  7. 为什么薄膜干涉的厚度要很小_薄膜干涉的薄膜为什么不能太厚?1.这里的厚是用什么来衡量的?2.假如一个厚为1mm的薄膜,是否薄?...
  8. R-CNN 《Rich Feature Hierachies for Accurate Object Detection and Semantic Segmentation》论文笔记
  9. POJ 3263-Tallest Cow
  10. Codeforces 1375H Set Merging (分块)