{"id":2579,"date":"2022-02-04T10:25:10","date_gmt":"2022-02-04T02:25:10","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2579"},"modified":"2022-02-04T10:25:11","modified_gmt":"2022-02-04T02:25:11","slug":"leetcodeday99","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday99\/","title":{"rendered":"leetcodeday99&#8211;\u6062\u590d\u4e8c\u53c9\u641c\u7d22\u6811\uff08!\uff09"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8be5\u6811\u4e2d\u7684&nbsp;<strong>\u6070\u597d<\/strong>&nbsp;\u4e24\u4e2a\u8282\u70b9\u7684\u503c\u88ab\u9519\u8bef\u5730\u4ea4\u6362\u3002<em>\u8bf7\u5728\u4e0d\u6539\u53d8\u5176\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\uff0c\u6062\u590d\u8fd9\u68f5\u6811&nbsp;<\/em>\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\/2020\/10\/28\/recover1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>root = &#91;1,3,null,null,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;3,1,null,null,2]\n<strong>\u89e3\u91ca\uff1a<\/strong>3 \u4e0d\u80fd\u662f 1 \u7684\u5de6\u5b69\u5b50\uff0c\u56e0\u4e3a 3 &gt; 1 \u3002\u4ea4\u6362 1 \u548c 3 \u4f7f\u4e8c\u53c9\u641c\u7d22\u6811\u6709\u6548\u3002<\/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\/2020\/10\/28\/recover2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>root = &#91;3,1,4,null,null,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;2,1,4,null,null,3]\n<strong>\u89e3\u91ca\uff1a<\/strong>2 \u4e0d\u80fd\u5728 3 \u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u4e3a 2 &lt; 3 \u3002\u4ea4\u6362 2 \u548c 3 \u4f7f\u4e8c\u53c9\u641c\u7d22\u6811\u6709\u6548\u3002<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"684\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-26-1024x684.png\" alt=\"\" class=\"wp-image-2582\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-26-1024x684.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-26-300x200.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-26-768x513.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-26.png 1429w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=99 lang=python3\r\n#\r\n# &#91;99] \u6062\u590d\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 recoverTree(self, root: Optional&#91;TreeNode]) -> None:\r\n        \"\"\"\r\n        Do not return anything, modify root in-place instead.\r\n        \"\"\"\r\n        firstNode = None\r\n        secondNode = None\r\n        pre = TreeNode(float(\"-inf\"))\r\n\r\n        stack = &#91;]\r\n        p = root\r\n        while p or stack:\r\n            while p:\r\n                stack.append(p)\r\n                p = p.left\r\n            p = stack.pop()\r\n            \r\n            if not firstNode and pre.val > p.val:\r\n                    firstNode = pre\r\n            if firstNode and pre.val > p.val:\r\n                #print(firstNode.val,pre.val, p.val)\r\n                secondNode = p\r\n            pre = p\r\n            p = p.right\r\n        firstNode.val, secondNode.val = secondNode.val, firstNode.val\r\n# @lc code=end\r\n\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"727\" height=\"278\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-25.png\" alt=\"\" class=\"wp-image-2580\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-25.png 727w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-25-300x115.png 300w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u8282\u70b9&nbsp;root&nbsp;\uff0c\u8be5\u6811\u4e2d\u7684&nbsp;\u6070\u597d&nbsp;\u4e24\u4e2a\u8282\u70b9\u7684\u503c\u88ab\u9519\u8bef &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday99\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday99&#8211;\u6062\u590d\u4e8c\u53c9\u641c\u7d22\u6811\uff08!\uff09<\/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\/2579"}],"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=2579"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2579\/revisions"}],"predecessor-version":[{"id":2583,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2579\/revisions\/2583"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2579"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}