二叉树的最大深度

31 阅读1分钟
package org.example.leetcodeBook.deepSearch;  
//二叉树的最大深度  
//给定一个二叉树 root ,返回其最大深度。  
//二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。  
public class MaxDepthSolution {  
    public int maxDepth(InorderTraversalSolution.TreeNode root){  
        if(root==null)return 0;  
        else{  
            int leftHeight = maxDepth(root.left);  
            int rightHeight = maxDepth(root.right);  
            return Math.max(leftHeight,rightHeight)+1;  
        }  
    }  
}