LeetCode二叉树的前序遍历、中序遍历、后序遍历、层序遍历、最大深度二叉树、最小深度二叉树、平衡二叉树、二叉树的直径Swift

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init() { self.val = 0; self.left = nil; self.right = nil; }
 *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
 *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
 *         self.val = val
 *         self.left = left
 *         self.right = right
 *     }
 * }
 */
class Solution {
    var res:[Int] = []
    func preorderTraversal(_ root: TreeNode?) -> [Int] {
        if root == nil {return []}
        res.append(root!.val)
        preorderTraversal(root!.left)
        preorderTraversal(root!.right)  
        return res
    }
}

 

概念

前序遍历:根节点-左子树-右子树

class Solution {
    var res:[Int] = []
    func preorderTraversal(_ root: TreeNode?) -> [Int] {
        if root == nil {return []}
        res.append(root!.val)
        preorderTraversal(root!.left)
        preorderTraversal(root!.right)  
        return res
    }
}

 

中序遍历:左子树-根节点-右子树

class Solution {
    var res:[Int] = []
    func inorderTraversal(_ root: TreeNode?) -> [Int] {
        if root == nil {return []}
        inorderTraversal(root!.left)
        res.append(root!.val)
        inorderTraversal(root!.right)  
        return res
    }
}

 

后序遍历:左子树-右子树-根节点

class Solution {
    var res:[Int] = []
    func postorderTraversal(_ root: TreeNode?) -> [Int] {
        if root == nil {return []}
        postorderTraversal(root!.left)
        postorderTraversal(root!.right)  
        res.append(root!.val)
        return res
    }
}

 

层序遍历:一层一层遍历

利用队列先进先出的原理,遍历二叉树,先把父节点加进去,然后移除父节点,把他的左右子节点加进去。

class Solution {
    func levelOrder(_ root: TreeNode?) -> [[Int]] {
        guard let root = root else { return [] }
        //最终结果数组
        var result:[[Int]] = []
        //根节点数组
        var queue:[TreeNode] = [root]
        while !queue.isEmpty {
            //存放根节点值得数组
            var tmpArr:[Int] = []
            for node in queue {
                //把根节点数组里的值放进去临时数组
                tmpArr.append(node.val)
                queue.removeFirst()
                if let node = node.left {
                    queue.append(node)
                }
                if let node = node.right {
                    queue.append(node)
                }
            }
            //把临时数组放进最终结果数组
            result.append(tmpArr)
        }
        return result
    }
}

 

 

 

 

深度优先遍历:

前序遍历:ABDECFG

中序遍历:DBEAFCG

后序遍历:DEBFGCA

广度优先遍历:

层序遍历:ABCDEFG

 

二叉树的最大深度

思路一:层序遍历总层数

class Solution {
    func maxDepth(_ root: TreeNode?) -> Int {
        guard let root = root else { return 0 }
        //根节点数组
        var queue:[TreeNode] = [root]
        var depth = 0
        while !queue.isEmpty {
            for node in queue {
                queue.removeFirst()
                if let node = node.left {
                    queue.append(node)
                }
                if let node = node.right {
                    queue.append(node)
                }
            }
            depth+=1
        }
        return depth
    }
}

思路二:二叉树的最大深度=Max(左子树的最大深度,右子树的最大深度)+1

    func maxDepth(_ root: TreeNode?) -> Int {
        guard let root = root else {
           return 0
        }
        return max(maxDepth(root.left), maxDepth(root.right)) + 1
     }

 

 

二叉树的最小深度

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init() { self.val = 0; self.left = nil; self.right = nil; }
 *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
 *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
 *         self.val = val
 *         self.left = left
 *         self.right = right
 *     }
 * }
 */
class Solution {
    func minDepth(_ root: TreeNode?) -> Int {
        guard let root = root else { return 0 }
        var queue:[TreeNode] = [root]
        var depth = 1
        while !queue.isEmpty {
            for node in queue {
                queue.removeFirst()
                if node.left == nil && node.right == nil {
                    return depth
                }
                if let node = node.left {
                    queue.append(node)
                }
                if let node = node.right {
                    queue.append(node)
                }
            }
            depth += 1
        }
        return depth
    }
}

 

平衡二叉树

一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1 。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init() { self.val = 0; self.left = nil; self.right = nil; }
 *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
 *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
 *         self.val = val
 *         self.left = left
 *         self.right = right
 *     }
 * }
 */
class Solution {
    func isBalanced(_ root: TreeNode?) -> Bool {
        guard let root = root else { return true }
        return self.isBalanced(root.left) && self.isBalanced(root.right) && abs(maxDepth(root.left)-maxDepth(root.right)) <= 1
    }
}

 

二叉树的直径

给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。

示例上面二叉树最大直径为[D,B,A,C,G]或者[D,B,A,C,F]或者[E,B,A,C,G]或者[E,B,A,C,F]。最大直径返回4。注意:两结点之间的路径长度是以它们之间边的数目表示。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.left = nil
 *         self.right = nil
 *     }
 * }
 */
class Solution {
    var maxLength = 0
    func diameterOfBinaryTree(_ root: TreeNode?) -> Int {
        guard let root = root else {
            return 0
        }
        let _ = maxDepth(root)
        return maxLength
    }
    func maxDepth(_ root: TreeNode?) -> Int {
        guard let root = root else {
            return 0
        }
        let leftDeepth = maxDepth(root.left)//左子树的最大深度
        let rightDeepth = maxDepth(root.right)//右子树的最大深度
        maxLength = max(maxLength, leftDeepth + rightDeepth)
        return max(leftDeepth, rightDeepth) + 1//当前结点的最大深度
    }
}

 

posted @ 2020-11-23 11:45  黄增松  阅读(128)  评论(0编辑  收藏  举报