Day 29 - LeetCode 104. Maximum Depth of Binary Tree

LeetCode 104. Maximum Depth of Binary Tree

題目

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

翻譯

給一個二元樹,找出它的深度

最大深度是從根節點, 到最遠點的最長距離數

Tree,  max = 3

        A
      /   \
    B       C
          /   \
        D       E

想法

!root 就 trturn 0,
自己呼叫自己(遞回), root.left節點 和root.right節點 的最大值

詳細參閱

https://discuss.leetcode.com/topic/80567/javascript-solution

https://discuss.leetcode.com/topic/79343/javascript-recursive-o-n


Code

/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {

    return find(root);

    function find(node) {
        if ( !node ) { 
            return 0;
        }
        let left = 1;
        let right = 1;
        if ( node.left ) {
            left+= find(node.left);
        }
        if ( node.right ) {
            right+= find(node.right);
        }
        return Math.max(left, right);
    }
};
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
    
    if ( !root ) {
        return 0;
    }
    let left = maxDepth(root.left) + 1;
    let right = maxDepth(root.right) + 1;

    return Math.max(left, right);
};
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {

    if ( !root ) {
        return 0;
    }
    return 1 + Math.max( maxDepth(root.left), maxDepth(root.right) );
};

Run

留言