列表

详情


104. 二叉树的最大深度

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

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

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

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

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

相似题目

平衡二叉树

二叉树的最小深度

N 叉树的最大深度

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * 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) { } };

golang 解法, 执行用时: 4 ms, 内存消耗: 4.2 MB, 提交时间: 2021-07-23 10:02:07

/**
 * 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 max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

javascript 解法, 执行用时: 96 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:48:28

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
    if ( root == null )
        return 0;
    var left = maxDepth(root.left);
    var right = maxDepth(root.right);
    if ( left > right )
        return left + 1;
    return right + 1;
};

c 解法, 执行用时: 16 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:46:45

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) {
    if ( root == NULL )
        return 0;
    int left = maxDepth(root->left);
    int right = maxDepth(root->right);
    if ( left > right )
        return left + 1;
    return right + 1;
}

cpp 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:44:36

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if ( root == NULL )
            return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return max(left, right) + 1;
    }
};

java 解法, 执行用时: 0 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:42:55

/**
 * 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;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        if ( left > right )
            return left + 1;
        return right + 1;
    }
}

golang 解法, 执行用时: 8 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:39:33

/**
 * 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
    }
    left := maxDepth(root.Left)
    right := maxDepth(root.Right)
    if left < right {
        return right + 1
    }
    return left + 1
}

python3 解法, 执行用时: 64 ms, 内存消耗: N/A, 提交时间: 2018-08-24 00:36:03

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

class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == None:
            return 0
        left = self.maxDepth(root.left)
        right = self.maxDepth(root.right)
        return max(left, right) + 1

上一题