Maximum Depth of Binary Tree

来源:互联网 发布:淘宝客服遇到的问题 编辑:程序博客网 时间:2024/06/10 03:03

https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/

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.

public int maxDepth(TreeNode root)


这题,真的需要解释么?有LEETCODE最高的通过率,是我第一个能够一行返回答案的题。。。


我就直接给代码吧:

    public int maxDepth(TreeNode root) {        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;    }

强烈不推荐BFS做,繁琐很多。简单来说就是统计往下递归最大的层数就是了。用bottom-top的方式返回结果。

0 0
原创粉丝点击