-
Notifications
You must be signed in to change notification settings - Fork 0
Convert sorted array to binary search tree #40
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
base: arai60
Are you sure you want to change the base?
Conversation
| if len(nums) == 1: | ||
| return TreeNode(nums[0]) | ||
|
|
||
| add_node = TreeNode(val=nums[len(nums)//2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第 1 引数のため、 val= は無くても良いと思います。
| return TreeNode(nums[0]) | ||
|
|
||
| add_node = TreeNode(val=nums[len(nums)//2]) | ||
| add_node.left = self.sortedArrayToBST(nums[:len(nums)//2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
スライスを作るとコピーが発生し、処理が重くなるため、 phase2 のようにインデックスで処理したほうがよいと思います。
| mid = (right + left) // 2 | ||
| node = TreeNode(nums[mid]) | ||
| node.left = build_bst(left, mid) | ||
| node.right = build_bst(mid+1, right) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- の両側にスペースを一つずつ空けることをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def build_bst(left, right): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
関数名が snake case だったり camel case だったりと統一されていないのが気になりました。
PEP 8 見る限りだと snake case が奨励されるみたいですね: https://peps.python.org/pep-0008/#function-and-variable-names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ああ、 Leetcode が指定する関数だから変えられないんですね。これは悩ましい...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分はleetcodeで用意されたmethod以外をコードで書くときはPEPに従ってsnake caseで書いています...
|
拝見しました。良いと思いました。 余裕があれば、loopやinorder-treeの解き方も試してみると良いと思います。 |
| def build_bst(left, right): | ||
| if left >= right: | ||
| return None | ||
| mid = (right + left) // 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここ以外の処理ではleft rightの順番となっております。
例えば引数であったり、nodeの処理部分
なので書き方を統一したらいいかと思いました。
ex) mid = (left + right) // 2
| @@ -0,0 +1,30 @@ | |||
| """ | |||
| Stack overflowしないかの一応の考察, height-balancedなので偏りがなく, 木の高さはlogn程度なので再帰の呼び出しは大丈夫そう | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一応、具体的な数字にしておくことをおすすめします。
Python の再帰の深さは限界いくらくらいで、n は今回いくつと考えているので、log n がいくつくらいになるのか、まで。
最後は、常に具体的な数字が問題になるので。(普段、わざわざ書くかどうかはともかく、練習ならば書いてもいいでしょう。)
問題
108. Convert Sorted Array to Binary Search Tree