Talking is cheap, show the codes.

Sequence traversal inverts the binary tree

swift code class TreeNode { var left: TreeNode? var right: TreeNode? var element: Int? } func invertBinaryTree(tempNode: TreeNode?) -> TreeNode? { guard let node = tempNode else { return tempNode } var queue = Array<TreeNode>() queue.append(node) while (queue.count > 0) {guard let node = queue.poplast () else {queue. Break} let temp = node.left node.left = node.right node.right = temp if let leftNode = node.left { queue.append(leftNode) } if let rightNode = node.right { queue.append(rightNode) } } return node }Copy the code

Quick sort

func quickSort(baseTo tempArr: inout Array<Int>, maxLenth: Int, begin : Int, end: Int) { var i: Int var j: Int if begin < end {I = begin + 1 j = end While I < j {if tempArr[I] > tempArr[begin] { Make the following swap // This swap ensures that the current value of j must be greater than the selected set value, Let temp = tempArr[I] tempArr[I] = tempArr[j] tempArr[j] = temp J -= 1} else {// Otherwise, the element at position I is less than the base value, and the element at position I += 1}} // To prevent the value at position I and the values before I are equal to the base value, While (tempArr[I] >= tempArr[begin]) {I -= 1; while (tempArr[I] >= tempArr[begin]) {I -= 1; } temparr.swapat (I, begin); QuickSort (baseTo: &tempArr, maxLenth: MaxLenth, begin: begin, end: I) // [baseTo: &tempArr, maxLenth: maxLenth, begin: j, end: end) } }Copy the code