-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path236_LowestCommonAncestorBinaryTree.swift
66 lines (62 loc) · 1.72 KB
/
236_LowestCommonAncestorBinaryTree.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
61
62
63
64
65
66
//
// 236_LowestCommonAncestorBinaryTree.swift
// LeetcodeSwift
//
// Created by yansong li on 2018-03-28.
// Copyright © 2018 YANSONG LI. All rights reserved.
//
import Foundation
/**
Title:236 Lowest Common Ancestor of a Binary Tree
URL: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
Space: O(n)
Time: O(n)
*/
func lowestCommonAncestorForRoot(_ root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode? {
let (resultNode, count) = lowestCommonAncestorForRootHelper(root, p: p, q: q, found: 0)
if count == 2 {
return resultNode
}
return nil
}
func lowestCommonAncestorForRootHelper(_ root: TreeNode,
p: TreeNode,
q: TreeNode,
found: Int) -> (TreeNode?, Int) {
var currentFound: Int = 0
if root.val == p.val || root.val == q.val {
if found == 1 {
return (root, 2)
}
currentFound = 1
}
var leftResult: TreeNode?
var leftFound: Int = 0
var rightResult: TreeNode?
var rightFound: Int = 0
if let left = root.left {
(leftResult, leftFound) = lowestCommonAncestorForRootHelper(left, p: p, q: q, found: currentFound)
if leftFound == 2 {
return (leftResult, leftFound)
}
}
if let right = root.right {
(rightResult, rightFound) = lowestCommonAncestorForRootHelper(right, p: p , q: q, found: currentFound)
if rightFound == 2 {
return (rightResult, rightFound)
}
}
if (currentFound + leftFound + rightFound) == 2 {
return (root, 2)
}
if leftFound == 1 {
return (leftResult, 1)
}
if rightFound == 1 {
return (rightResult, 1)
}
if currentFound == 1 {
return (root, 1)
}
return (nil, 0)
}