美文网首页
leetcode-二叉树的最大深度

leetcode-二叉树的最大深度

作者: 8239e604d437 | 来源:发表于2018-12-22 10:06 被阅读0次

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

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

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

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

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

代码

/**
 * 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;
    }
  
    let leftMaxDepth = root.left?maxDepth(root.left):0;
    let rightMaxDepth = root.right?maxDepth(root.right):0;
    let max = leftMaxDepth>rightMaxDepth?leftMaxDepth:rightMaxDepth;
    
    return 1+max ;
};

相关文章

  • 二叉树面试题基本问题

    二叉树的最大深度与最小深度 二叉树的最大深度 最大深度是指二叉树根节点到该树叶子节点的最大路径长度。而最小深度自然...

  • 二叉树最大最小深度的递归非递归实现

    一、二叉树最大深度 二叉树的最大深度是根节点到叶子节点的最大长度 1.1 最大深度的递归实现 传入根节点,得到左右...

  • 104. Maximum Depth of Bianry Tre

    题目 求二叉树的最大深度 解析 二叉树的最大深度,是左子树深度加 1 和右子树深度加 1 的最大值。即 f(nod...

  • 二叉树的深度系列

    LeetCode 104 二叉树的最大深度 题目 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节...

  • LeetCode-104-二叉树的最大深度

    LeetCode-104-二叉树的最大深度 104. 二叉树的最大深度[https://leetcode-cn.c...

  • LeetCode | 树相关题目

    LeetCode 104.二叉树的最大深度 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最...

  • LeetCode 深度优先遍历

    概述 前言 104 二叉树的最大深度【简单】 111 二叉树的最小深度 【简单】 124 二叉树中的最大路径和 【...

  • leetcode-二叉树的最大深度

    给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没...

  • leetcode上二叉树和递归 java

    二叉树天然的递归结构104. 二叉树的最大深度 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节...

  • Swift 二叉树的最大深度- LeetCode

    题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点...

网友评论

      本文标题:leetcode-二叉树的最大深度

      本文链接:https://www.haomeiwen.com/subject/mnfikqtx.html