Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linked List] Add a solution to Convert Sorted List to Binary Search … #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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