完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25
问题描述:

问题求解:
单纯遍历了一遍,emmm,果然TLE。
解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << h - 1即可,如果不是,则递归的计算左右子树的个数。
时间复杂度:O(logn * logn)
public int countNodes(TreeNode root) {
if (root == null) return 0;
int l = leftHeight(root);
int r = rightHeight(root);
if (l == r) return (1 << l) - 1;
else return 1 + countNodes(root.left) + countNodes(root.right);
}
private int leftHeight(TreeNode root) {
if (root == null) return 0;
return 1 + leftHeight(root.left);
}
private int rightHeight(TreeNode root) {
if (root == null) return 0;
return 1 + rightHeight(root.right);
}
2019-04-15 14:42:44
其实我根本不用管二叉树的种类,直接递归去计数就可以了,在log(n)的时间复杂度内完成,且速度完胜之前的解法。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Count Complete Tree Nodes.
public int countNodes(TreeNode root) {
if (root == null) return 0;
return countNodes(root.left) + countNodes(root.right) + 1;
}
完全二叉树的节点个数 Count Complete Tree Nodes的更多相关文章
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes
解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) ; TreeNode *pLeft = root->left; TreeNod ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 222. Count Complete Tree Nodes -- 求完全二叉树节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- Redhat7.4 vim编辑无颜色解决
背景 发现使用vim时, 统一颜色, 不区分关键字 解决 vim /etc/profile 在文件的末尾加入下面这一行 export TERM=xterm
- 简单的windows窗口创建实例
#include<windows.h> #include<tchar.h> LRESULT CALLBACK WndProc(HWND hwnd,UINT umsg,WPARA ...
- 树之105 Construct Binary Tree from Preorder and Inorder Traversal
题目链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 参考链 ...
- 封装QML能访问的类
一.常用宏 1.信号与槽 C++类中的信号与槽都可以在QML中访问 2.C++类的成员函数,Q_INVOKABLE Q_INVOKABLE void function(); 3.C++类的枚举,Q_E ...
- 创建一个maven项目
创建父工程 1.新建maven project,点击next 2.默认配置,点击next 3.默认配置,点击next 4.填写Group Id一般采用域名倒写,Artifact Id为项目名称.然后点 ...
- linux下安装tomcat和jdk
1.现在的linux服务器一般自带jdk,先查询是否已经安装jdk rpm -qa | grep java rpm -qa | grep jdk 如上则是没有安装,可以直接跳到步骤X,安装jdk.否则 ...
- Master of GCD 【线段树区间更新 || 差分】
Master of GCD 时间限制: 1 Sec 内存限制: 128 MB 提交: 670 解决: 112 [提交] [状态] [命题人:admin] 题目描述 Hakase has n num ...
- Btrfs管理及应用
一.btrfs基本概念 btrfs文件系统是2007年Oracle开发,支持GPL协议,为了取代Linux早期的ext系列文件系统. btrfs核心特性: 多物理卷支持:btrfs可由多个底层物理卷组 ...
- 再谈fedora下的音乐和视频播放器的安装
rpm包就相当于windows下的exe,已经是编译后的二进制代码,可以使用rpm命令或dnf install ???.rpm来安装 lnux下软件的安装跟windows不同, 后者要到处去找, 要做 ...
- 网络_TCP连接的建立与释放
三报文握手 1.概述 TCP是面向连接的协议.TCP建立连接的过程叫做握手,握手需要在客户和服务器之间交换三个TCP报文段,即我们说的"三次握手"(严格讲是一次握手过程中交换了三个 ...