-
Notifications
You must be signed in to change notification settings - Fork 2
/
226.Invert-Binary-Tree.swift
47 lines (42 loc) · 1.35 KB
/
226.Invert-Binary-Tree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//: 226 Invert Binary Tree
/**
* Question Link: https://leetcode.com/problems/invert-binary-tree/
*/
/**
* 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 {
// Preorder invert
func invertTree_pre(_ root: TreeNode?) -> TreeNode? {
guard let root = root else { return nil }
// Swap the children of the current node first, then recursively inverting its left and right child
let temp = root.left
root.left = root.right
root.right = temp
invertTree(root.left)
invertTree(root.right)
return root
}
// Postorder invert
func invertTree_post(_ root: TreeNode?) -> TreeNode? {
guard let root = root else { return nil }
// Recursively calling the inverting of the left and right child of the node first, then doing the actual swap.
let left = invertTree(root.left)
let right = invertTree(root.right)
root.left = right
root.right = left
return root
}
}