计算二叉树的深度 (计算二叉树的深度)

 

思路:

// @Title: 计算二叉树的深度 (计算二叉树的深度)
// @Author: qisiii
// @Date: 2022-03-01 20:59:35
// @Runtime: 0 ms
// @Memory: 40.9 MB
// @comment: 
// @flag: 
/**
 * 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 Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18