首页 > 其他 > 详细

[leetcode]104. Maximum Depth of Binary Tree二叉树最大深度

时间:2019-05-14 10:18:54      阅读:112      评论:0      收藏:0      [点我收藏+]

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.

 

题意:

求二叉树最大深度

 

Solution1: DFS

 

code

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int maxDepth(TreeNode root) {
12         if(root == null) return 0;
13        
14         int maxLeft = maxDepth(root.left);
15         int maxRight = maxDepth(root.right);
16     
17         return Math.max(maxLeft, maxRight)+1;
18     }
19 }

 

[leetcode]104. Maximum Depth of Binary Tree二叉树最大深度

原文:https://www.cnblogs.com/liuliu5151/p/10860179.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!