LeetCode之Maximum Depth of Binary Tree
发布日期:2021-06-29 14:08:16 浏览次数:2 分类:技术文章

本文共 570 字,大约阅读时间需要 1 分钟。

1、题目

 

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

to see which companies asked this question.

 

 

 

2、代码实现

 

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int maxDepth(TreeNode root) {        if (root == null)             return 0;        return  1 + Math.max(maxDepth(root.left), maxDepth(root.right));    }}

 

 

 

 

 

 

转载地址:https://chenyu.blog.csdn.net/article/details/69663267 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode之Happy Number
下一篇:LeetCode之Sum of Two Integers

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月25日 06时26分57秒