0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift で LCA: Lowest Common Ancestor(最近共通祖先)

Last updated at Posted at 2021-09-17

備忘メモ

NOTE

コード

func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? {
  // Return null if not find nodes we are looking for.
  func dfs(_ root: TreeNode?) -> TreeNode? {
    if root == nil { return nil }
    if root?.val == p?.val || root?.val == q?.val { return root } // current is node we are looking for.
    let left  = dfs(root?.left)
    let right = dfs(root?.right)
    if left == nil && right == nil { return nil } // if not find node in children.
    if left != nil && right != nil { return root } // if current is LCA.
    return left == nil ? right : left // return either if one child found node.
  }
  
  return dfs(root)
}

ポイント

dfs がreturnする TreeNode だが、再帰的に呼び出されてる中で途中で意味合いがことなる。それを1つのTreeNodeという表現かつロジックで書いてあるのでやや混乱した。

再帰的に呼び出されてる dfs がreturn する値の意味は下記の2つのどちらかのケースになる。

  1. 探し当てた p または q の位置
  2. LCA の位置

TMP-3 2.jpg

  1. の通りp か q のどちらか1つを見つけたなら return するのはその見つけた p か q になるが、2) のようにchildren の両方ともが p と q を見つけてきてる場合はこのノードがLCAになる。なので、そこから上へ渡すのはLCAになる。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?