算法练习day20

81 阅读1分钟

一、修剪二叉搜索树

当发现节点的值小于最小值,还需要判断该节点的右节点

当发现节点的值大于最大值,还需要判断该节点的左节点

递归法

/**
 * 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
 * @param {number} low
 * @param {number} high
 * @return {TreeNode}
 */
var trimBST = function(root, low, high) {
    if(!root) {
        return root
    }
    if(root.val < low) {
        return trimBST(root.right, low, high)
    }
    if(root.val > high) {
        return trimBST(root.left, low, high)
    }
    root.left = trimBST(root.left, low, high)
    root.right = trimBST(root.right, low, high)
    return root
};

迭代法


var trimBST = function (root, low, high) {
    if (!root) {
        return root
    }
    while (root && (root.val < low || root.val > high)) {
        if (root.val < low) {
            root = root.right
        }
        if (root.val > high) {
            root = root.left
        }
    }
    let cur = root
    while (cur) {
        while (cur.left && cur.left.val < low) {
            cur.left = cur.left.right
        }
        cur = cur.left
    }
    cur = root
    while (cur) {
        while (cur.right && cur.right.val > high) {
            cur.right = cur.right.left
        }
        cur = cur.right
    }
    return root
};

二、将有序数组转为二叉搜索树

递归法

/**
 * 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 {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function(nums) {
    if(!nums.length) {
        return null
    }
    let midIndex = Math.floor(nums.length / 2)
    let midNode = new TreeNode(nums[midIndex])
    midNode.left = sortedArrayToBST(nums.slice(0, midIndex))
    midNode.right = sortedArrayToBST(nums.slice(midIndex + 1))
    return midNode
};

迭代法

用两个队列分别代表当前node和,当前操作子数组

var sortedArrayToBST = function (nums) {
    if (!nums.length) {
        return null
    }
    let root = new TreeNode(0)
    let numsQueue = [nums]
    let nodeQueue = [root]
    while (nodeQueue.length) {
        let arr = numsQueue.shift()
        let node = nodeQueue.shift()
        let midIndex = Math.floor(arr.length / 2)
        node.val = arr[midIndex]
        let leftArr = arr.slice(0, midIndex)
        let rightArr = arr.slice(midIndex + 1)
        if (leftArr.length) {
            let leftNode = new TreeNode(0)
            node.left = leftNode
            numsQueue.push(leftArr)
            nodeQueue.push(leftNode)
        }
        if (rightArr.length) {
            let rightNode = new TreeNode(0)
            node.right = rightNode
            numsQueue.push(rightArr)
            nodeQueue.push(rightNode)
        }
    }
    return root
};

三、把二叉搜索树转换为累加树

递归法,反中序遍历

/**
 * 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 {TreeNode}
 */
var convertBST = function(root) {
    let preVal = 0
    function dfs(root) {
        if(!root) {
            return 0
        }
        dfs(root.right)
        root.val += preVal
        preVal = root.val
        dfs(root.left)
    }
    dfs(root)
    return root
};

迭代法

var convertBST = function(root) {
    let preVal = 0
    let stack = []
    let cur = root
    while(cur || stack.length) {
        if(cur) {
            stack.push(cur)
            cur = cur.right
        } else {
            let node = stack.pop()
            node.val += preVal
            preVal = node.val
            cur = node.left
        }
    }
    return root
};