首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
二叉树的最大宽度
[编程题]二叉树的最大宽度
热度指数:1559
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个二叉树,你需要编写一个函数来获取这课树的最大宽度,
二叉树的最大宽度是指具有节点数最多的那一层的结点个数。
示例1
输入
{1,2,3}
输出
2
示例2
输入
{1,2,3,4,5,#,6}
输出
3
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(5)
分享
纠错
提交结果有问题?
0个回答
0篇题解
添加回答
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
上传者:
小小
难度:
0条回答
5收藏
1850浏览
热门推荐
通过挑战的用户
郁闷的小白菜在...
2023-03-10 21:04:08
gaotian...
2023-03-07 21:32:24
K乀
2023-03-04 17:13:55
已注销
2023-02-26 19:47:08
GeekExp...
2023-02-16 14:26:37
相关试题
下列针对Cortex-M3和Cor...
单片机
评论
(1)
Choose the best w...
英语语法
评论
(1)
假如张三使用机器制造一个螺丝帽直径...
机器学习
评论
(1)
小刘去服装店购买了一批衣服,他先给...
数学运算
评论
(3)
在一个 Spring Boot 应...
Spring
评论
(1)
二叉树的最大宽度
扫描二维码,关注牛客网
意见反馈
下载牛客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 getMaxWidth (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 getMaxWidth(TreeNode* 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 getMaxWidth(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 getMaxWidth (TreeNode head) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求这可树的最大宽度 * @param head TreeNode类 树的根节点 * @return int整型 */ function getMaxWidth( head ) { // write code here } module.exports = { getMaxWidth : getMaxWidth };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求这可树的最大宽度 * @param head TreeNode类 树的根节点 * @return int整型 */ function getMaxWidth( $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 getMaxWidth(self , head ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求这可树的最大宽度 * @param head TreeNode类 树的根节点 * @return int整型 */ func getMaxWidth( head *TreeNode ) int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求这可树的最大宽度 * @param head TreeNode类 树的根节点 * @return int整型 */ int getMaxWidth(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 getMaxWidth(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 getMaxWidth(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 getMaxWidth(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 getMaxWidth (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 getMaxWidth(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 getMaxWidth ( _ 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 getMaxWidth(&self, head: Option
>) -> i32 { // write code here } }
{1,2,3}
2
{1,2,3,4,5,#,6}
3