LeetCode 104. Maximum Depth of Binary Tree 题解

来源:互联网 发布:在线看熊片的软件 编辑:程序博客网 时间:2024/05/01 18:32
题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/?tab=Description
思路:二叉树的最大深度= max{ 左子树的最大深度,右子树的最大深度} + 1

Java代码如下:
public class Solution {// 二叉树的最大深度等于max {左子树的最大深度,右子树的最大深度} + 1    public int maxDepth(TreeNode root) {        if(root == null){        return 0;        }         return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;       }}

0 0
原创粉丝点击