( Leetcode 104 ) Maximum Depth of Binary Tree

来源:互联网 发布:网贷数据交易平台 编辑:程序博客网 时间:2024/05/17 00:54

题目: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.

解题思路:

这是一道很简单常用的数据结构题,简单来说通过递归方法就可以求出来。下面直接看代码吧。


Java代码:

public class Solution {   public int maxDepth( TreeNode root ) {        if( root == null ) {        return 0;        }        int ldep = maxDepth( root.left );int rdep = maxDepth( root.right );return ldep > rdep ? ldep+1 : rdep+1;    }}

程序运行时间 0 ms。

0 0
原创粉丝点击