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

 

思路:

// @Title: 二叉树的最大深度 (Maximum Depth of Binary Tree)
// @Author: qisiii
// @Date: 2024-09-14 23:36:49
// @Runtime: 0 ms
// @Memory: 41.8 MB
// @comment: 
// @flag: 
/**
 * 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) {
        if(root==null){
            return 0;
        }
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
}

思路:

// @Title: 二叉树的最大深度 (Maximum Depth of Binary Tree)
// @Author: qisiii
// @Date: 2022-02-28 20:22:24
// @Runtime: 0 ms
// @Memory: 40.7 MB
// @comment: 
// @flag: 
/**
 * 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) {
        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