{"id":2478,"date":"2022-01-29T15:07:37","date_gmt":"2022-01-29T07:07:37","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2478"},"modified":"2022-01-29T15:07:38","modified_gmt":"2022-01-29T07:07:38","slug":"leetcodeday108","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/29\/leetcodeday108\/","title":{"rendered":"leetcodeday108 &#8211;\u5c06\u6709\u5e8f\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u5176\u4e2d\u5143\u7d20\u5df2\u7ecf\u6309&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u6392\u5217\uff0c\u8bf7\u4f60\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u68f5&nbsp;<strong>\u9ad8\u5ea6\u5e73\u8861<\/strong>&nbsp;\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/p>\n\n\n\n<p><strong>\u9ad8\u5ea6\u5e73\u8861&nbsp;<\/strong>\u4e8c\u53c9\u6811\u662f\u4e00\u68f5\u6ee1\u8db3\u300c\u6bcf\u4e2a\u8282\u70b9\u7684\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u7684\u9ad8\u5ea6\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u300d\u7684\u4e8c\u53c9\u6811\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/18\/btree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>nums = &#91;-10,-3,0,5,9]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;0,-3,9,-10,null,5]\n<strong>\u89e3\u91ca\uff1a<\/strong>&#91;0,-10,5,null,-3,null,9] \u4e5f\u5c06\u88ab\u89c6\u4e3a\u6b63\u786e\u7b54\u6848\uff1a\n<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/18\/btree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>nums = &#91;1,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;3,1]\n<strong>\u89e3\u91ca\uff1a<\/strong>&#91;1,3] \u548c &#91;3,1] \u90fd\u662f\u9ad8\u5ea6\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=108 lang=python3\r\n#\r\n# &#91;108] \u5c06\u6709\u5e8f\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811\r\n#\r\n\r\n# @lc code=start\r\n# Definition for a binary tree node.\r\n# class TreeNode:\r\n#     def __init__(self, val=0, left=None, right=None):\r\n#         self.val = val\r\n#         self.left = left\r\n#         self.right = right\r\nclass Solution:\r\n    def sortedArrayToBST(self, nums: List&#91;int]) -> TreeNode:\r\n       \r\n        def sortedArray(nums,root,rootindex,rindex,lindex):\r\n            leftv=(rootindex+lindex)\/\/2\r\n            rightv=(rootindex+rindex+1)\/\/2\r\n            print(leftv,rightv,rootindex)\r\n            if leftv!=rootindex:\r\n                root.left=TreeNode(nums&#91;leftv],None,None)\r\n                x=lindex\r\n                y=rootindex-1\r\n                z=leftv\r\n                sortedArray(nums,root.left,z,y,x)\r\n            else:root.left=None\r\n            if rightv!=rootindex:\r\n                root.right=TreeNode(nums&#91;rightv],None,None)\r\n                l=rootindex+1\r\n                m=rightv\r\n                n=rindex \r\n                sortedArray(nums,root.right,m,n,l)\r\n            else:root.right=None\r\n        lens=len(nums)-1\r\n        root = TreeNode(nums&#91;lens\/\/2],None,None)\r\n        sortedArray(nums,root,lens\/\/2,lens,0)  \r\n        return root  \r\n# @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"203\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-309-1024x203.png\" alt=\"\" class=\"wp-image-2479\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-309-1024x203.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-309-300x59.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-309-768x152.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-309.png 1039w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\uff0c\u5176\u4e2d\u5143\u7d20\u5df2\u7ecf\u6309&nbsp;\u5347\u5e8f&nbsp;\u6392\u5217\uff0c\u8bf7\u4f60\u5c06\u5176\u8f6c\u6362 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/29\/leetcodeday108\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday108 &#8211;\u5c06\u6709\u5e8f\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2478"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2478\/revisions"}],"predecessor-version":[{"id":2482,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2478\/revisions\/2482"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}