20200707——第一百零四題 二叉樹的最大深度

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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章