Skip to content

Conversation

@LucasXu0
Copy link

Add a solution to Convert Sorted List to Binary Search Tree

@LucasXu0
Copy link
Author

please check this PR.

while cur != nil {
sortedArr.append(cur!.val)
cur = cur!.next
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you can do something like this below, so you don't have to implicitly unwrap the variable which is not recommended in production code?

        while let newCurr = cur {
            sortedArr.append(newCurr.val)
            cur = newCurr.next
        }

private func _helper(_ sortedArr: [Int], _ left: Int, _ right: Int) -> TreeNode? {
guard left <= right else { return nil }

let mid = left + (right - left) >> 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the >> operation is so cool! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants