226. Invert Binary Tree--LeetCode Record

Tong_hdj發表於2016-07-08

這裡寫圖片描述

/**
 * 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 {
    func invertTree(root: TreeNode?) -> TreeNode? {
        if root == nil {
            return nil
        }
        let swapNode = invertTree(root?.left)
        root?.left = invertTree(root?.right)
        root?.right = swapNode

        return root
    }
}

相關文章