首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
二叉树的深度
[编程题]二叉树的深度
热度指数:388629
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度,根节点的深度视为 1 。
数据范围:节点的数量满足
,节点上的值满足
进阶:空间复杂度
,时间复杂度
假如输入的用例为{1,2,3,4,5,#,6,#,#,7},那么如下图:
示例1
输入
{1,2,3,4,5,#,6,#,#,7}
输出
4
示例2
输入
{}
输出
0
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(334)
邀请回答
收藏(1145)
分享
提交结果有问题?
1175个回答
240篇题解
开通博客
牛客题解官
发表于 2020-06-01 11:06:59
精华题解
题目的主要信息: 给定一棵二叉树的根节点,求这棵树的最大深度 深度是指树的根节点到任一叶子节点路径上节点的数量 最大深度是所有叶子节点的深度的最大值 叶子节点是指没有子节点的节点 举一反三: 学习完本题的思路你可以解决如下题目: JZ82. 二叉树中和为某一值的路径(一) JZ28. 对称的二叉
展开全文
LaN666
发表于 2021-06-22 23:17:26
精华题解
38、二叉树的深度 解题思路: 题目是 从根节点到叶节点的路径,所以就是求出二叉树的层数即可。 方法一:层次遍历 我们先来回顾一下二叉树的层次遍历,一般我们都是用队列去实现的。 步骤: 1、先创建一个队列,将根节点入队; 2、队列不为空,进入循环: 出队一个节点 将当前节点的左右节点入队(不为空时
展开全文
江南好___
发表于 2021-06-22 21:33:10
精华题解
描述 题目描述 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 示例 输入:{1,2,3,4,5,#,6,#,#,7} 返回值:4引言 求解二叉树相关的题目,一般都可以使用遍历(如层序遍历)或递归(自底向上递归)的方法解决。
展开全文
幸福的火龙果在干饭
发表于 2021-06-22 19:21:22
精华题解
一、题目描述 JZ38二叉树的深度题目大意:给定一棵二叉树,求该数的高度 二、算法1(自底向上递归) 算法思路 1.总体思路:根据题意,树的深度是从根节点到叶子结点的所有路径中最长路径的长度,要到达叶子结点,要么走左子树要么走右子树,因此对于以某个结点为根节点构成的子树来说,它所表示的二叉树的深度就
展开全文
FYZ~
发表于 2019-08-08 10:56:43
二叉树:二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。题目:求二叉树的深度,从根节点到字节点的最长路径。递归求法: public int TreeDepth(Tre
展开全文
ZwZ呀咿呀咿哟
发表于 2020-02-06 11:52:24
看题解上没有介绍使用层次遍历计算二叉树的深度的思路,那我就介绍一下吧。思路: 借助队列,对二叉树进行层次遍历; 在层次遍历的过程中,每次当队列中某一层的节点出队完成后,高度+1; 关键点:判别队列中某一层节点出队完成的标准是什么? 在出队之前,此时队列中记录的只有某一层节点,所以队列的大小就是某一
展开全文
一叶浮尘
发表于 2019-08-23 08:50:13
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 此题用递归的思想做就非常简单了。 public class Solution { public int TreeDepth(TreeNode root) {
展开全文
数据结构和算法
发表于 2021-03-15 23:26:25
1,递归 这题最容易想到的就是递归,啥叫“递归”,也就是下面这张图 开个玩笑,我们画个图来看下 原理很简单,代码如下 public int TreeDepth(TreeNode root) { return root==null? 0 : Math.max(TreeDepth(
展开全文
ziffer
发表于 2021-07-21 16:23:07
public class Solution { public int TreeDepth(TreeNode root) { return root == null ? 0 : Math.max(TreeDepth(root.left), TreeDepth(root.righ
展开全文
Ironxin
发表于 2020-03-10 11:42:10
思路1:递归写法构思:传入某节点,调用该方法,返回的应该是以传入节点为根节点的树的深度,而树的深度,肯定和左右子树深度有关,所以进入这个方法后,就包含了左右子树的深度(而要得到左右子树的深度,肯定又是以左右子节点为根节点,再次调用该方法深度获取的,因此此时进行递归),并且还有由一个左右深度比较的过程
展开全文
牛客ID:923045198
发表于 2020-03-01 10:20:28
递归方法 # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = No
展开全文
白伟仝
发表于 2020-06-08 11:58:27
import java.util.*; class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this
展开全文
陌上锦衣卫
发表于 2020-05-04 22:38:27
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。class Solution {public: int TreeDepth(TreeNode* pRoot) { if(pRoot!=NULL) retur
展开全文
加油做题
发表于 2022-06-12 22:17:29
int TreeDepth(struct TreeNode* pRoot ) { if(pRoot==NULL){ return 0; } int depth; int left_depth=TreeDepth(pRoot->left);
展开全文
问题信息
树
来自:
“一战通offer”互...
难度:
1175条回答
1145收藏
114859浏览
热门推荐
通过挑战的用户
查看代码
GH9805
2023-03-08 11:34:16
Elastic...
2023-02-05 21:34:05
牛客44742...
2022-09-16 16:14:53
躺平了的大魔王很理智
2022-09-16 14:40:45
讲道理的小饼干...
2022-09-16 11:49:22
相关试题
对称的二叉树
树
评论
(1125)
来自
“一战通offer”互联...
二进制中1的个数
基础数学
评论
(2447)
来自
“一战通offer”互联...
和为S的两个数字
数组
数学
双指针
评论
(1513)
来自
“一战通offer”互联...
6个不同式样的珠子,可以串联成()...
产品
运营
游戏策划
设计
牛客
财务
审计
税务服务
风险管理
证券分析师
理财顾问
柜面服务
营销
项目助理
评论
(1)
来自
牛客模拟卷—行测篇A卷
二叉树的深度
扫描二维码,关注牛客网
意见反馈
下载牛客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 pRoot TreeNode类 * @return int整型 */ public int TreeDepth (TreeNode pRoot) { // 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 pRoot TreeNode类 * @return int整型 */ int TreeDepth(TreeNode* pRoot) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return int整型 # class Solution: def TreeDepth(self , pRoot ): # 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 pRoot TreeNode类 * @return int整型 */ public int TreeDepth (TreeNode pRoot) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ function TreeDepth( pRoot ) { // write code here } module.exports = { TreeDepth : TreeDepth };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ function TreeDepth( $pRoot ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return int整型 # class Solution: def TreeDepth(self , pRoot: TreeNode) -> int: # write code here
package main import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ func TreeDepth( pRoot *TreeNode ) int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ int TreeDepth(struct TreeNode* pRoot ) { // 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 pRoot TreeNode类 # @return int整型 # class Solution def TreeDepth(pRoot) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ def TreeDepth(pRoot: TreeNode): Int = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return int整型 */ fun TreeDepth(pRoot: 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 pRoot TreeNode类 * @return int整型 */ public int TreeDepth (TreeNode pRoot) { // 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 pRoot TreeNode类 * @return int整型 */ export function TreeDepth(pRoot: 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 pRoot TreeNode类 * @return int整型 */ func TreeDepth ( _ pRoot: 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 pRoot TreeNode类 * @return int整型 */ pub fn TreeDepth(&self, pRoot: Option
>) -> i32 { // write code here } }
{1,2,3,4,5,#,6,#,#,7}
4
{}
0