算法 - 二叉树05(Swift版本)

65 阅读1分钟

题目1: 654. 最大二叉树

讲解  
leetcode

经过昨天的中序 后序遍历构造二叉树, 这个题写起来还是挺轻松的。 这次尝试了一下左闭右开。

class Solution {
    func constructMaximumBinaryTree(_ nums: [Int]) -> TreeNode? {
        // 左闭右开
        return buildTree(nums, 0, nums.count)
    }

    func buildTree(_ nums: [Int], _ left: Int, _ right: Int) -> TreeNode? {
        if right - left <= 0 { return nil }
        var maxIndex = left
        for i in left..<right {
            if nums[i] > nums[maxIndex] {
                maxIndex = i
            }
        }
        let node = TreeNode(nums[maxIndex])
        node.left = buildTree(nums, left, maxIndex)
        node.right = buildTree(nums, maxIndex + 1, right)
        return node
    }

}

题目2: 617. 合并二叉树

讲解  
leetcode

class Solution {
    func mergeTrees(_ root1: TreeNode?, _ root2: TreeNode?) -> TreeNode? {
        guard let root1 else { return root2 }
        guard let root2 else { return root1 }

        root1.val += root2.val
        root1.left = mergeTrees(root1.left, root2.left)
        root1.right = mergeTrees(root1.right, root2.right)
        return root1
    }
}

题目3: 700.二叉搜索树中的搜索

讲解  
leetcode


// 此写法忽略了搜索树的性质
class Solution {
    func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
        guard let root else { return nil }
        if root.val == val { return root }
        if let left = searchBST(root.left, val) { return left }
        if let right = searchBST(root.right, val) { return right }
        return nil
    }
}

// 递归法
class Solution {
    func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
        guard let root else { return nil }
        if root.val == val { return root }
        if val < root.val, let left = searchBST(root.left, val) { return left }
        if val > root.val, let right = searchBST(root.right, val) { return right }
        return nil
    }
}

// 迭代法
class Solution {
    func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
        var node = root
        while let curNode = node {
            if val > curNode.val { node = curNode.right }
            else if val < curNode.val { node = curNode.left }
            else { return curNode }
        }
        return nil
    }
}

题目4:98.验证二叉搜索树

讲解    
leetcode

迭代法使用的还是 迭代法大一统模版。


// 递归法
class Solution {
    var preNode: TreeNode?
    func isValidBST(_ root: TreeNode?) -> Bool {
        guard let root else { return true }
        let leftValid = isValidBST(root.left)
        if let preNode, root.val <= preNode.val { return false }
        preNode = root
        let rightValid = isValidBST(root.right)
        return leftValid && rightValid
    }
}

// 迭代法
class Solution {
    var preNode: TreeNode?
    func isValidBST(_ root: TreeNode?) -> Bool {
        guard let root else { return true }
        var stack: [TreeNode?] = [root]
        while !stack.isEmpty {
            let node = stack.removeLast()
            if let node {
                if let right = node.right { stack.append(right) }
                stack.append(node)
                stack.append(nil)
                if let left = node.left { stack.append(left) }
            } else {
                let trueNode = stack.removeLast()
                if let preNode, trueNode?.val ?? 0 <= preNode.val {
                    return false
                }
                preNode = trueNode
            }
        }
        return true
    }
}