博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
104. Maximum Depth of Binary Tree - Easy
阅读量:7286 次
发布时间:2019-06-30

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

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

3   / \  9  20    /  \   15   7

return its depth = 3.

 

time: O(logn), space: O(logn)

/** * 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;        }        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));    }}

 

转载于:https://www.cnblogs.com/fatttcat/p/10189258.html

你可能感兴趣的文章
oracle查看表空间大小及表数量
查看>>
js 常用提示 console.log & console.info
查看>>
php stdClass 转数组
查看>>
优化NGINX的25种手段
查看>>
svn安装
查看>>
动态容器 数组 api
查看>>
抽取IPA包里的所有图片,包括.car压缩包中的文件
查看>>
DFS lock handle等待事件
查看>>
PowerDesigner 反转Java代码生成类图
查看>>
iOS 分割线设置
查看>>
MyBatis insert 返回主键的方法
查看>>
分布式文件系统FastDFS原理介绍
查看>>
IOS基础知识
查看>>
ubuntu 13.10 SVN配置(ubuntu通用)
查看>>
vim常用技巧
查看>>
关于继承类执行构造函数的顺序问题
查看>>
ps详解
查看>>
SpringMVC用responsebody标签返回json的时候Error406
查看>>
Python开发SVN批量提交命令脚本
查看>>
关于IE的bug(CSS)
查看>>