104. Maximum Depth of Binary Tree(二叉树的最大深度)

题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/

思路:

深度遍历,保存最大值。

AC 0ms 100% Java:

/**
 * 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) {
        return helper(root,0);
    }
    public int helper(TreeNode root,int depth){
        if(root==null)
            return depth;
        return Math.max(helper(root.left,depth+1),helper(root.right,depth+1));
    }
}

猜你喜欢

转载自blog.csdn.net/God_Mood/article/details/88642723