Skip to main content
Tweeted twitter.com/StackCodeReview/status/1495956696551804931
added 4 characters in body
Source Link
thadeuszlay
  • 4k
  • 29
  • 53

The taskThe task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

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

const iterateTree = (root, level) => {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

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

const iterateTree = (root, level) => {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

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

const iterateTree = (root, level) => {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};
deleted 10 characters in body
Source Link
thadeuszlay
  • 4k
  • 29
  • 53

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

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

const iterateTree = function(root, level) => {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
const maxDepth = function(root) {
    if (root === null) {
        return 0;
    }
    return iterateTree(root, 1);
};

const iterateTree = function(root, level) {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

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

const iterateTree = (root, level) => {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};
Source Link
thadeuszlay
  • 4k
  • 29
  • 53

Maximum Depth of Binary Tree in JavaScript

The task

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3

      3
     / \
    /   \
   9    20
        / \
       15  7

Example 2: Input: root = [1,null,2] Output: 2

My solution:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
const maxDepth = function(root) {
    if (root === null) {
        return 0;
    }
    return iterateTree(root, 1);
};

const iterateTree = function(root, level) {
    const isLeftTreeMissing = root.left === null;
    const isRightTreeMissing = root.right === null
    
    let leftLevel = level;
    let rightLevel = level;
    
    if (isLeftTreeMissing === false) {
        leftLevel = iterateTree(root.left, level + 1);
    }
    if (isRightTreeMissing === false) {
        rightLevel = iterateTree(root.right, level + 1);
    }
    return leftLevel > rightLevel ? leftLevel : rightLevel;
};