首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
完全二叉树结点数
[编程题]完全二叉树结点数
热度指数:10307
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一棵完全二叉树的头节点head,返回这棵树的节点个数。
完全二叉树指:设二叉树的深度为h,则 [1,h-1] 层的节点数都满足
个
数据范围:节点数量满足
,节点上每个值都满足
进阶:空间复杂度
, 时间复杂度
示例1
输入
{1,2,3}
输出
3
示例2
输入
{}
输出
0
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(4)
邀请回答
收藏(167)
分享
提交结果有问题?
47个回答
27篇题解
开通博客
不会做题的小菜鸡
发表于 2021-07-17 14:55:45
精华题解
思路 最直观的思路就是将所有的结点数一遍,这样得到最终结果的时间代价就是O(N) 上面一个方法忽略了我们的树是完全二叉树这一性质,完全二叉树满足的特点就是要么是一个满二叉树,要么除了最后一层以外其它层全满,最后一层的叶子结点必须从左到右不间隔的排布。 虽然完全二叉树没有计算结点的方法 但是满二叉树
展开全文
江南好___
发表于 2021-07-18 18:08:52
精华题解
描述 题目描述 给定一棵完全二叉树的头节点head,返回这棵树的节点个数。如果完全二叉树的节点数为N,请实现时间复杂度低于O(N)的解法。 示例 输入:{1,2,3} 返回值:3知识点:完全二叉树,递归难度:⭐⭐⭐ 题解 方法一:递归 解题思路: 完全二叉树的特性: 完全二叉树的左右子树中至
展开全文
棒棒糖🍭201906101800876
发表于 2021-07-15 19:11:00
精华题解
nc84. 求完全二叉树的高度 1. 思路一: (暴力做法, 不合要求) 直接递归统计二叉树的节点个数即可. 空树没有节点, 加上左子树和右子树的节点数就可以. class Solution { public: int nodeNum(struct TreeNode* head) {
展开全文
草狐想
发表于 2021-01-11 16:32:47
思路: 先理解下面两点: 完全二叉树的子树也是完全二叉树 完全二叉树的左右子树中至少有一颗是满二叉树 计算一颗满二叉树节点个数: 1.计算一颗满二叉树节点个数很简单,就等于2^h - 1 , h为该满二叉树的高度 问题在于如何确定那颗子树是满二叉树 1.如果左子树的高度等于右子树高度+1,
展开全文
摸鱼学大师
发表于 2021-07-16 15:21:28
思路: 最简单的办法莫过于遍历所有的结点,统计个数,但是不管是哪种遍历,时间复杂度都要求至少为O(n),因为要遍历每一个结点。 方法一:遍历法 不符合复杂度要求!!! 方法二:二分法(利用性质) 由此,我们可以从完全二叉树的性质下手: 完全二叉树叶子结点只能出现在最下两层 最下层的叶子一定集中在左
展开全文
总之就是非常可爱
发表于 2022-02-27 15:11:06
/** struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x)
展开全文
总之就是非常可爱
发表于 2022-02-27 15:18:38
/** struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x)
展开全文
Maokt
发表于 2021-07-29 13:16:04
算法思想一:递归 解题思路: 完全二叉树的结点数量:根节点+左子树结点数量+右子树结点数 因此可以想到采用递归算法计算二叉树根结点的左右子树结点数量再加上根节点数量 代码展示: Python版本 class Solution: &n
展开全文
姐姐的遮阳伞
发表于 2022-03-27 14:09:27
import java.util.*; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) {
展开全文
小小小明哥
发表于 2022-01-19 03:38:10
该题目可以用二叉树的Morris遍历来实现空间复杂度为o(1),时间复杂度为o(n) /** struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int
展开全文
猫头鹰的咖啡馆
发表于 2021-10-27 16:38:22
/** struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(
展开全文
编程小鹏
发表于 2024-03-30 09:06:04
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int v
展开全文
Mr.galaxy
发表于 2022-11-22 21:55:20
">#include<vector> #include<algorithm> struct Tree { int val; struct T
展开全文
问题信息
二分
树
来自:
面试常考算法题(二)
难度:
47条回答
167收藏
8706浏览
热门推荐
通过挑战的用户
查看代码
牛客11510...
2022-11-29 15:05:15
『戥』
2022-09-19 15:42:09
牛客83664...
2022-09-16 10:26:43
。。20190...
2022-09-16 00:27:19
ariou
2022-09-15 11:34:10
相关试题
分组
基础数学
二分
评论
(11)
航海
排序
思维
二分
评论
(1)
远亲不如近邻
排序
二分
评论
(13)
体育课测验(二)
广度优先搜索(BFS)
拓扑排序
dfs
评论
(2)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ public int nodeNum (TreeNode head) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ int nodeNum(TreeNode* head) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head TreeNode类 # @return int整型 # class Solution: def nodeNum(self , head ): # write code here
using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ public int nodeNum (TreeNode head) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ function nodeNum( head ) { // write code here } module.exports = { nodeNum : nodeNum };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ function nodeNum( $head ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head TreeNode类 # @return int整型 # class Solution: def nodeNum(self , head: TreeNode) -> int: # write code here
package main import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ func nodeNum( head *TreeNode ) int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ int nodeNum(struct TreeNode* head ) { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head TreeNode类 # @return int整型 # class Solution def nodeNum(head) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ def nodeNum(head: TreeNode): Int = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ fun nodeNum(head: TreeNode?): Int { // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ public int nodeNum (TreeNode head) { // write code here } }
/*class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ export function nodeNum(head: TreeNode): number { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ func nodeNum ( _ head: TreeNode?) -> Int { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head TreeNode类 * @return int整型 */ pub fn nodeNum(&self, head: Option
>) -> i32 { // write code here } }
{1,2,3}
3
{}
0