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

102 阅读1分钟

知识讲解

题目1: 二叉树的遍历递归

讲解

题目2: 二叉树的迭代遍历

讲解

1. 前序遍历

// 递归法
class Solution {
    var res = [Int]()
    func preorderTraversal(_ root: TreeNode?) -> [Int] {
        preorderRecursive(root)
        return res
    }

    func preorderRecursive(_ root: TreeNode?) {
        guard let root else { return }
        res.append(root.val)
        preorderRecursive(root.left)
        preorderRecursive(root.right)
    }
}

// 迭代法
class Solution {
    func preorderTraversal(_ root: TreeNode?) -> [Int] {
        var stack = [root]
        var res = [Int]()
        while !stack.isEmpty {
            guard let node = stack.removeLast() else { continue }
            res.append(node.val)
            stack.append(node.right)
            stack.append(node.left)
        }
        return res
    }
}

2. 中序遍历

// 递归法
class Solution {
    var res = [Int]()
    func inorderTraversal(_ root: TreeNode?) -> [Int] {
        inorderRecursive(root)
        return res
    }

    func inorderRecursive(_ root: TreeNode?) {
        guard let root else { return }
        inorderRecursive(root.left)
        res.append(root.val)
        inorderRecursive(root.right)
    }
}

// 迭代法
class Solution {
    func inorderTraversal(_ root: TreeNode?) -> [Int] {
        var res = [Int]()
        var stack = [TreeNode]()
        var cur = root
        while cur != nil || !stack.isEmpty {
            if cur != nil {
                stack.append(cur!)
                cur = cur?.left
            } else {
                let node = stack.removeLast()
                res.append(node.val)
                cur = node.right
            }
        }

        return res
    }
}

3. 后序遍历

// 递归法
class Solution {
    var res = [Int]()
    func postorderTraversal(_ root: TreeNode?) -> [Int] {
        postorRecusive(root)
        return res
    }

    func postorRecusive(_ root: TreeNode?) {
        guard let root else { return }
        postorRecusive(root.left)
        postorRecusive(root.right)
        res.append(root.val)
    }
}

// 迭代法
class Solution {
    func postorderTraversal(_ root: TreeNode?) -> [Int] {
        var res = [Int]()
        var stack = [root]
        while !stack.isEmpty {
            guard let node = stack.removeLast() else { continue }
            res.append(node.val)
            stack.append(node.left)
            stack.append(node.right)
        }
        res.reverse()
        return res
    }
}

题目3: 二叉树的统一迭代法

讲解

思路:脑子里遍历了一遍, 每个值前面都会被增加一次nil标记。 移除标记后,直接将元素加入结果集合就行了。

class Solution {
    func preorderTraversal(_ root: TreeNode?) -> [Int] {
        var stack = [TreeNode?]()
        var res = [Int]()
        if root != nil { stack.append(root) }
        while !stack.isEmpty {
            if let node = stack.removeLast() {
                // 根左右的 逆序 -  右左根
                if node.right != nil { stack.append(node.right) }
                if node.left != nil { stack.append(node.left) }
                stack.append(node)
                // 添加标记
                stack.append(nil)
            } else {
                // 碰到标记
                if let node = stack.removeLast() {
                    res.append(node.val)
                }
            }
        }
        return res
    }
}

题目4: 二叉树层序遍历登场!

讲解

层序遍历 简直就是大脑按摩仪, 很好理解。

class Solution {
    func levelOrder(_ root: TreeNode?) -> [[Int]] {
        var queue = [TreeNode]()
        var res = [[Int]]()
        if let root { queue.append(root) }
        while !queue.isEmpty {
            let size = queue.count
            var line = [Int]()
            for i in 0..<size {
                let node = queue.removeFirst()
                line.append(node.val)
                if let left = node.left {
                    queue.append(left) 
                }               
                if let right = node.right {
                    queue.append(right) 
                }              
            }
            res.append(line)
        }
        return res
    }
}