Algorithm - LeetCode Problems 22

이소라·2024년 1월 16일
0

Algorithm

목록 보기
76/77

Problem 111. Minimum Depth of Binary Tree

  • Given a binary tree, find its minimum depth.

  • The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

  • Note: A leaf is a node with no children.

Examples

  • Example 1:

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

    • Input: root = [2,null,3,null,4,null,5,null,6]
    • Output: 5

Constraints

  • The number of nodes in the tree is in the range [0, 105].
  • -1000 <= Node.val <= 1000

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}
 */
var minDepth = function(root) {
    let answer = 50000;
    const queue = [[root, 1]];

    if (!root) {
        return 0;
    }

    while (queue.length) {
        const [node, depth] = queue.shift();
        if (depth >= answer) continue;
        if (!node.left && !node.right) answer = depth;
        if (node.left) queue.push([node.left, depth + 1]);
        if (node.right) queue.push([node.right, depth + 1]);
    }
    return answer;
};



Problem 1448. Count Good Nodes in Binary Tree

  • Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.

  • Return the number of good nodes in the binary tree.

Examples

  • Example 1:

    • Input: root = [3,1,4,3,null,1,5]
    • Output: 4
    • Explanation: Nodes in blue are good.
      • Root Node (3) is always a good node.
      • Node 4 -> (3,4) is the maximum value in the path starting from the root.
      • Node 5 -> (3,4,5) is the maximum value in the path
      • Node 3 -> (3,1,3) is the maximum value in the path.
  • Example 2:

    • Input: root = [3,3,null,4,2]
    • Output: 3
    • Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
  • Example 3:
    • Input: root = [1]
    • Output: 1
    • Explanation: Root is considered as good.

Constraints:

  • The number of nodes in the binary tree is in the range [1, 10^5].
  • Each node's value is between [-10^4, 10^4].

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}
 */
var goodNodes = function(root) {
    let answer = 0;
    const queue = [[root, root.val]];

    if (!root) {
        return 0;
    }

    while (queue.length) {
        let [node, maxValue] = queue.shift();
        if (node.val >= maxValue) {
            answer++;
            maxValue = node.val;
        }
        if (node.left) queue.push([node.left, maxValue]);
        if (node.right) queue.push([node.right, maxValue]);
    }

    return answer;
};



Problem 1026. Maximum Difference Between Node and Ancestor

  • Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.

  • A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.

Examples

  • Example 1:
    • Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
    • Output: 7
    • Explanation: We have various ancestor-node differences, some of which are given below :
      • |8 - 3| = 5
      • |3 - 7| = 4
      • |8 - 1| = 7
      • |10 - 13| = 3
      • Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.

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

Constraints

  • The number of nodes in the tree is in the range [2, 5000].
  • 0 <= Node.val <= 10^5

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}
 */
var maxAncestorDiff = function(root) {
    let diff = 0;
    let minValue = 100000;
    let maxValue = 0;

    const dfs = (node, minValue, maxValue) => {
        minValue = node.val < minValue ? node.val : minValue;
        maxValue = node.val > maxValue ? node.val : maxValue;
        if (!node.left && !node.right) {
            diff = Math.max(diff, maxValue - minValue);
            return;
        }
        if (node.left) dfs(node.left, minValue, maxValue);
        if (node.right) dfs(node.right, minValue, maxValue);
    }

    dfs(root, minValue, maxValue);
    return diff;
};

0개의 댓글