LeetCode刷题 Day21

69 阅读2分钟

LeetCode刷题 Day21

530. Minimum Absolute Difference in BST

Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

Example 1:

Input: root = [4,2,6,1,3]
Output: 1

Example 2:

Input: root = [1,0,48,null,null,12,49]
Output: 1

思路:

  • 依靠BST中序遍历的特性 来记录minDiff 和 prevValue

代码:

var getMinimumDifference = function(root) {
    let minDiff = Number.POSITIVE_INFINITY;
    let prev = -1;
    
    var helper = function(root) {
        if (!root) return;
        
        helper(root.left);
        if (prev !== -1 && minDiff > Math.abs(root.val - prev)) {
            minDiff = Math.abs(root.val - prev)
        }
        
        prev = root.val;
        helper(root.right);
    }
    
    helper(root);
    return minDiff;
};

时间复杂度: O(N) 空间复杂度: O(N)


501. Find Mode in Binary Search Tree

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

 

Example 1:

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

Example 2:

Input: root = [0]
Output: [0]

思路:

  • 通过遍历题目中设定的BST来计算各个value的freq

代码:

var findMode = function(root) {
    let cache = {};
    let maxCount = 0;
    let res = [];
    var helper = function(root) {
        if (!root) return;
        
        helper(root.left);
        cache[root.val] = cache[root.val] ? ++cache[root.val] : 1;
        maxCount = Math.max(maxCount, cache[root.val]);
        helper(root.right);
    }
    
    helper(root);
    
    for (let key of Object.keys(cache)) {
        if (cache[key] === maxCount) res.push(key);
    }
    
    return res;
};

时间复杂度: O(N) 空间复杂度: O(N)


236. Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

思路:

  • 经典LCA, 关键在于后续遍历部分, 当left 和 right都不为空即说明left, right说明p和q都被找到了 那么root作为最近的ancestor节点即为结果

代码:

var lowestCommonAncestor = function(root, p, q) {
    if (!root) return null;
    if (root === p || root === q) return root;
    
    const left = lowestCommonAncestor(root.left, p, q);
    const right = lowestCommonAncestor(root.right, p, q);
    
    if (left && right) return root;
    if (left && !right) return left;
    if (!left && right) return right;
};

时间复杂度: O(N), 最坏情况是遍历所有节点。 空间复杂度: O(N), 同样当为skewer binary tree的时候 可能需要保存所有节点