{"id":2935,"date":"2020-02-14T11:18:38","date_gmt":"2020-02-14T03:18:38","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2935"},"modified":"2020-02-14T11:18:38","modified_gmt":"2020-02-14T03:18:38","slug":"%e3%80%90leetcode%e3%80%910105%e5%89%91%e6%8c%87offer07-%e9%87%8d%e5%bb%ba%e4%ba%8c%e5%8f%89%e6%a0%91","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2935\/","title":{"rendered":"\u3010LeetCode\u30110105&amp;\u5251\u6307offer07  \u91cd\u5efa\u4e8c\u53c9\u6811"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/zhong-jian-er-cha-shu-lcof\/\">https:\/\/leetcode-cn.com\/problems\/zhong-jian-er-cha-shu-lcof\/<\/a><\/p>\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/construct-binary-tree-from-preorder-and-inorder-traversal\/\">https:\/\/leetcode-cn.com\/problems\/construct-binary-tree-from-preorder-and-inorder-traversal\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em># Definition for a binary tree node.\nclass TreeNode(object):\n    def __init__(self, x):\n        self.val = x\n        self.left = None\n        self.right = None\nclass Solution(object):\n    def buildTree(self, preorder, inorder):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> preorder: List[int]\n        <\/em><strong><em>:type<\/em><\/strong><em> inorder: List[int]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: TreeNode\n        \"\"\"\n        <\/em>def iter_node(temp_pre_order, temp_in_order):\n            if len(temp_in_order) == 0:\n                return\n            if len(temp_in_order) == 1:\n                return TreeNode(temp_pre_order[0])\n            root_idx = temp_in_order.index(temp_pre_order[0])\n            root = TreeNode(temp_pre_order[0])\n            root.left = iter_node(temp_pre_order[1:], temp_in_order[:root_idx])\n            root.right = iter_node(temp_pre_order[root_idx + 1:], temp_in_order[root_idx + 1:])\n            return root\n        root = iter_node(preorder, inorder)\n        return root\nif __name__ == '__main__':\n    preorder = [3, 9, 20, 15, 7]\n    inorder = [9, 3, 15, 20, 7]\n    # preorder = [1, 2, 3, 4, 5]\n    # inorder = [3, 2, 1, 4, 5]\n    print(Solution().buildTree(preorder, inorder))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u5206\u522b\u8ba1\u7b97\u5de6\u53f3\u5b50\u6811\u76f4\u81f3\u53ea\u6709\u5355\u4e00\u7ed3\u70b9\u65f6\u8fd4\u56de\u3002<\/p>\n\n\n<p>\u6839\u636e\u524d\u4e2d\u904d\u5386\u7684\u601d\u60f3\uff0c\u9996\u5148\u662f\u6839\u636e\u524d\u5e8f\u904d\u5386\u5207\u5272\u4e2d\u5e8f\u904d\u5386\uff0c\u7136\u540e\u5411\u4e0b\u8ba1\u7b97\u5b50\u8282\u70b9\u3002\u5c06\u8fd4\u56de\u5206\u522b\u94fe\u5230\u6839\u8282\u70b9\u7684\u5de6\u53f3\u5b50\u6811\u5373\u53ef\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"273\" src=\"\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-42.png\" alt=\"\" class=\"wp-image-2936\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-42.png 574w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-42-300x143.png 300w\" sizes=\"(max-width: 574px) 100vw, 574px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/zhong-jian-er-cha [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1509,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2935"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2935"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2935\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2935"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}