二叉树的最大深度 🌟🌟🌟🌟🌟简单

课后作业

问题描述

原文链接:104. 二叉树的最大深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

代码实现

Java

/**
 * 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 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}

Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0

        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) {
            return 0;
        }

        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};

Go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }

    return 1 + max(maxDepth(root.Left), maxDepth(root.Right))
}

func max(a, b int) int {
    if a > b {
        return a
    }

    return b
}

发表回复

后才能评论

评论(5)

  • Mr.肖 永久会员 2024年4月9日 下午5:42
    /**
    打卡
     * 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;
            }
    
            int max = Math.max(maxDepth(root.left),maxDepth(root.right));
            return max + 1;
        }
    }
    
  • Yuki 永久会员 2024年2月3日 下午1:53

    打卡
    class Solution {
    public:
    int maxDepth(TreeNode* root) {
    if(rootnullptr)return 0;
    int left=maxDepth(root->left);
    int right=maxDepth(root->right);
    return max(left,right)+1;
    }
    };

  • 泡芙 永久会员 2023年9月22日 上午10:07
    • Time:O(n),N 为树的节点数量,计算树的深度需要遍历所有节点
    • Space:O(n),最差情况下(当树退化为链表时),递归深度可达到 n
    class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            // 此树深度=左子树的深度与右子树的深度中的最大值+1
            return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
        }
    }
    
  • 阿尚 普通 2023年8月21日 上午10:37
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null) return 0;
    
            int left = maxDepth(root.left);//左子树最大深度
            int right = maxDepth(root.right);//右子树最大深度
    
            return Math.max(left, right) + 1;
        }
    }
    
  • 帅气的冬瓜 普通 2023年7月5日 下午2:15

    打卡

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理