LeetCode刷题 Day20

60 阅读1分钟

LeetCode刷题 Day20

654. Maximum Binary Tree

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

Example 1:

Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
    - The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
        - Empty array, so no child.
        - The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
            - Empty array, so no child.
            - Only one element, so child is a node with value 1.
    - The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
        - Only one element, so child is a node with value 0.
        - Empty array, so no child.

Example 2:

Input: nums = [3,2,1]
Output: [3,null,2,null,1]

思路:

  • 这是一个构造树的题目, 所以要想到new TreeNode, 和treeNode.left treeNode.right的构造
  • 找到最大值和最大值的index,根据index左右分割数组
  • 当left > right时,就是临界条件
var constructMaximumBinaryTree = function(nums) {
    let left = 0;
    let right = nums.length - 1;
    
    var helper = function(left, right) {
        let maxValue = Number.NEGATIVE_INFINITY;
        let maxIndex = -1;
        if (left > right) return null;
        for (let i = left; i <= right; i++) {
            if (maxValue < nums[i]) {
                 maxValue = nums[i];
                 maxIndex = i;
            }
        }

        const treeNode = new TreeNode(maxValue);
        treeNode.left = helper(left, maxIndex - 1);
        treeNode.right = helper(maxIndex + 1, right);
        
        return treeNode;
    }
    
    return helper(left, right);
};

时间复杂度: O(n2) 查找最大值 * 递归, 空间复杂度: O(n)


617. Merge Two Binary Trees

You are given two binary trees root1 and root2.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

Return the merged tree.

Note:  The merging process must start from the root nodes of both trees.

 

Example 1:

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output: [3,4,5,5,4,null,7]

Example 2:

Input: root1 = [1], root2 = [1,2]
Output: [2,2]

思路:

  • 构造新二叉树
  • 当其中root1为空 返回root2, 反之亦然

代码:

var mergeTrees = function(root1, root2) {
    if (!root1) return root2;
    if (!root2) return root1;

    const treeNode = new TreeNode(root1.val + root2.val);
    
    treeNode.left = mergeTrees(root1.left, root2.left);
    treeNode.right = mergeTrees(root1.right, root2.right);
    return treeNode;
};

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


700. Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

  Example 1:

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

Example 2:

Input: root = [4,2,7,1,3], val = 5
Output: []

思路:

  • 可以通过对比root.val 与 target value的大小进行剪枝
var searchBST = function(root, val) {
    if (!root) return null;
    
    if (root.val === val) return root; 
    else if (root.val > val) {
        return searchBST(root.left, val);
    }
    else if (root.val < val) {
        return searchBST(root.right, val);
    } 
};

时间复杂度: 这个可以看成二分,N(logN), 但是如果是链状BST, 则为O(n)。 空间复杂度: O(N)


98. Validate Binary Search Tree

Given the root of a binary tree, determine if it is a valid binary search tree (BST) .

valid BST is defined as follows:

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

Example 1:

Input: root = [2,1,3]
Output: true

Example 2:

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

思路:

  • 一个辅助函数,利用BST中序遍历特性

代码:

var isValidBST = function(root) {
    var prev = Number.NEGATIVE_INFINITY;
    var res = true;
    var helper = function(root) {
        if (!root) return;
        
        helper(root.left);
        if (prev >= root.val) res = false;
        prev = root.val;
        helper(root.right);
    }
    
    helper(root);
    return res;
};

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