104. 二叉树的最大深度 + 深搜

104. 二叉树的最大深度

LeetCode_104

题目描述

代码实现

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        return dfs(root);
    }
    public int dfs(TreeNode root){
        if(root == null)
            return 0;
        return Math.max(dfs(root.left), dfs(root.right)) + 1;
    }
}
posted @ 2021-03-22 19:21  Garrett_Wale  阅读(34)  评论(0编辑  收藏  举报