Day2:二叉树的最大深度

leetcode链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

递归的做法:

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

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

小白刷Leetcode 文章被收录于专栏

那些必刷的leetcode

全部评论

相关推荐

对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务