Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You have a simple tree structure Ⓐ and its clone ⓐ.
- Each node in the tree has a pointer to it's parent as well as an array of its children.
- Given an original tree's node Ⓑ and cloned tree ⓐ, implement a method that returns ⓑ (the clone of Ⓑ).
- (Imagine finding the matching UIButton/UISlider/UIView in a separate cloned view controller.)
- Original
- Ⓐ
- ┏━┻━━┓
- ◯ ◯
- ┏┻┓ ┏━╋━┓
- ◯ ◯ ◯ ◯ ◯
- ┏┻┓ ┃
- ◯ Ⓑ ◯
- Clone
- ⓐ
- ┏━┻━━┓
- ◯ ◯
- ┏┻┓ ┏━╋━┓
- ◯ ◯ ◯ ◯ ◯
- ┏┻┓ ┃
- ◯ ⓑ ◯ */
- func findClonevView(_ cloneView: UIView, _ root: UIView) -> UIView{
- var runner = cloneView
- var arr = [Int]()
- while runner.superView != nil {
- let temp = runner
- runner = runner.superView!
- if let index = runner.subViews.firstIndex(where: { (item) -> Bool in
- item === temp
- }){
- arr.append(index)
- }
- }
- runner = root
- while arr.count > 0 {
- let curr = arr.removeLast()
- runner = runner.subViews[curr]
- }
- return runner
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement