-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
60 lines (45 loc) · 1.7 KB
/
Contents.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
48
49
50
51
52
53
54
55
56
57
58
59
60
//: [上一道题](@previous)
/*:
# 二叉树的最近公共祖先
- 题号:[236](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
- 难度:中等
- 描述:
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:
“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,
满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
*/
//: ## Code
import Foundation
func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
var result: TreeNode? = nil
// 遍历树
recurseTree(root, p, q, &result);
return result
}
func recurseTree(_ currentNode: TreeNode?, _ p: TreeNode?, _ q: TreeNode?, _ result: inout TreeNode?) -> Bool {
// 如果到达分支的末尾,则返回 false
guard let currentNode = currentNode else {
return false
}
let left = recurseTree(currentNode.left, p, q, &result) ? 1 : 0
let right = recurseTree(currentNode.right, p, q, &result) ? 1 : 0
let mid = (currentNode == p || currentNode == q) ? 1 : 0
if mid + left + right >= 2 {
result = currentNode
}
return mid + left + right > 0
}
//: ## Test
//if let dfs = TreeNode(dfs: [1,2,3,4,nil,nil,5,nil,nil,nil,6,7,nil,8,nil,nil,9]) {
// print(dfs.bfsDescription)
// print(dfs.dfsDescription)
//}
//
//print("------------------------------------------------")
//
//if let bfs = TreeNode(bfs: [3,5,1,6,2,0,8,nil,nil,7,4]) {
// print(bfs.bfsDescription)
// print(bfs.dfsDescription)
//}
//: [下一道题](@next)