{"id":226,"date":"2023-08-14T15:19:42","date_gmt":"2023-08-14T07:19:42","guid":{"rendered":"http:\/\/lingbo.online\/?p=226"},"modified":"2024-03-19T16:27:31","modified_gmt":"2024-03-19T08:27:31","slug":"leecode_tree","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/algorithm_learning\/leecode_tree\/","title":{"rendered":"Leecode\u7b97\u6cd5\u603b\u7ed3\u2014\u2014\u6811"},"content":{"rendered":"<h3>\u4e8c\u53c9\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u3001\u5c42\u5e8f\u904d\u5386<\/h3>\n<p>\u6709\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/tree1.png\" alt=\"\" \/><br \/>\n\u5219\u524d\u3001\u4e2d\u3001\u540e\u904d\u5386\u5bf9\u5e94\u7684\u89c4\u5219\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a<\/p>\n<ul>\n<li>\u524d\u5e8f\u904d\u5386\uff1a\u5148\u6839\u3001\u518d\u5de6\u3001\u518d\u53f3\uff0c\u5373\uff1a<strong>ABDEGCF<\/strong><\/li>\n<li>\u4e2d\u5e8f\u904d\u5386\uff1a\u5148\u5de6\u3001\u518d\u6839\u3001\u518d\u53f3\uff0c\u5373<strong>DBGEACF<\/strong><\/li>\n<li>\u540e\u5e8f\u904d\u5386\uff1a\u5148\u5de6\u3001\u518d\u53f3\u3001\u518d\u6839\uff0c\u5373\uff1a<strong>DGEBFCA<\/strong><\/li>\n<li>\u5c42\u5e8f\u904d\u5386\uff1a\u6309\u6811\u7684\u5c42\u6b21\u641c\u7d22\uff0c\u5373\uff1a<br \/>\n<strong>ABCDEFG<\/strong><\/li>\n<\/ul>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<p>\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u7684\u904d\u5386\u53ef\u4ee5\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u89e3\u51b3\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6216\u8005\u5355\u8c03\u6808\u89e3\u51b3\u3002\u9012\u5f52\u51fd\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u5b9e\u73b0\u9690\u5f0f\u7684\u5229\u7528\u4e86\u88ab\u79f0\u4e3a<strong>\u8c03\u7528\u6808\u7684\u6808<\/strong>\uff0c\u5373\u9012\u5f52\u5229\u7528\u4e86\u6808\uff0c\u53ea\u662f\u9690\u5f0f\u7684\u5229\u7528\u4e86\u6808\uff0c\u6ca1\u6709\u663e\u793a\u7684\u8ba9\u4f60\u770b\u5230\u5176\u4f7f\u7528\u4e86\u6808\u3002\u4ee5\u4e2d\u5e8f\u904d\u5386\u4e3a\u4f8b\uff0c\u6574\u4f53\u8fc7\u7a0b\u4e3a<strong>\u8bbf\u95ee\u7ed3\u70b9\u5e76\u5165\u6808\u904d\u5386\u5de6\u5b50\u6811\uff0c\u51fa\u6808\u904d\u5386\u53f3\u5b50\u6811<\/strong>\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u8f85\u52a9\u6808\u5c06\u4e0a\u9762\u9012\u5f52\u51fd\u6570\u9690\u5f0f\u8c03\u7528\u6808\u7684\u8fc7\u7a0b\u663e\u793a\u8868\u793a\u3002\u4ee5\u4e2d\u5e8f\u904d\u5386\u4e3a\u4f8b\uff0c\u5b9e\u73b0<strong>\u8bbf\u95ee\u7ed3\u70b9\u5e76\u5165\u6808\u904d\u5386\u5de6\u5b50\u6811\uff0c\u7ed3\u70b9\u51fa\u6808\u904d\u5386\u53f3\u5b50\u6811\u3002<\/strong><\/p>\n<p>\u5c42\u5e8f\u904d\u5386\u53ef\u4ee5\u4f7f\u7528\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\u89e3\u51b3\uff0c\u5373\u6784\u9020\u4e00\u4e2a\u8f85\u52a9\u961f\u5217\uff0c\u5f53\u961f\u5217\u4e0d\u4e3a\u7a7a\u65f6\uff0c<strong>\u5f39\u51fa\u5f53\u524d\u8282\u70b9\uff0c\u5e76\u5c06\u5f53\u524d\u8282\u70b9\u7684\u5de6\u53f3\u8282\u70b9\u52a0\u5165\u5230\u961f\u5217\u4e2d\u3002<\/strong><\/p>\n<h4>\u6848\u4f8b\u4ee3\u7801<\/h4>\n<h5>\u9012\u5f52\u601d\u8def<\/h5>\n<pre><code class=\"language-python\">#\u524d\u5e8f\u904d\u5386\nclass Solution:\n    def preorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        def preorder(root: TreeNode):\n            if not root:\n                return\n            res.append(root.val)\n            preorder(root.left)\n            preorder(root.right)\n\n        res = list()\n        preorder(root)\n        return res\n\n# \u4e2d\u5e8f\u904d\u5386\nclass Solution:\n    def inorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        def inorder(root: TreeNode):\n            if not root:\n                return\n            inorder(root.left)\n            res.append(root.val)\n            inorder(root.right)\n\n        res = list()\n        inorder(root)\n        return res\n\n# \u540e\u5e8f\u904d\u5386\nclass Solution:\n    def postorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        def postorder(root: TreeNode):\n            if not root:\n                return\n            postorder(root.left)\n            postorder(root.right)\n            res.append(root.val)\n\n        res = list()\n        postorder(root)\n        return res<\/code><\/pre>\n<h5>\u8fed\u4ee3\u601d\u8def<\/h5>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6808\u8fdb\u884c\u8fed\u4ee3\uff0c\u4ee5\u524d\u5e8f\u904d\u5386\u4e3a\u4f8b\uff0c\u8fc7\u7a0b\u8868\u793a\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u6808\uff0c\u5c06\u6839\u8282\u70b9\u5165\u6808<\/li>\n<li>\u5f53\u6808\u4e0d\u4e3a\u7a7a\u65f6\uff1a\n<ul>\n<li>\u5f39\u51fa\u6808\u9876\u5143\u7d20<code>Node<\/code>\uff0c\u5e76\u5c06\u503c\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\uff1b<\/li>\n<li>\u5982\u679c<code>Node<\/code>\u7684\u53f3\u5b50\u6811\u975e\u7a7a\uff0c\u5c06\u53f3\u5b50\u6811\u5165\u6808<\/li>\n<li>\u5982\u679c<code>Node<\/code>\u7684\u5de6\u5b50\u6811\u975e\u7a7a\uff0c\u5c06\u5de6\u5b50\u6811\u5165\u6808<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/preorder.gif\" alt=\"\" \/><\/p>\n<pre><code class=\"language-python\">#\u524d\u5e8f\u904d\u5386\nclass Solution:\n    def preorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        if not root:\n            return []\n        stack,res=[root],[]\n        while stack:\n            node=stack.pop()\n            if node:\n                res.append(node)\n                if node.right:\n                    stack.append(node.right)\n                if node.left:\n                    stack.append(node.left)\n        return res\n\n# \u4e2d\u5e8f\u904d\u5386\nclass Solution:\n    def inorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        if not root:\n            return []\n        cur,stack,res=root,[],[]\n        while stack or cur:\n            while cur: #cur\u5165\u6808\uff0c\u5e76\u5230\u8fbe\u6700\u5de6\u7aef\u7684\u53f6\u5b50\u7ed3\u70b9\n                stack.append(cur)\n                cur=cur.left\n            tmp=stack.pop()\n            res.append(tmp.val)\n            cur=tmp.right\n        return res\n\n# \u540e\u5e8f\u904d\u5386\nclass Solution:\n    def inorderTraversal(self, root: TreeNode) -&gt; List[int]:\n        if not root:\n            return []\n        cur,stack,res=root,[],[]\n        while stack or cur:\n            while cur: #\u6700\u5148\u5230\u8fbe\u53f3\u7aef\n                res.append(cur.val)\n                stack.append(cur)\n                cur=cur.right\n            tmp=stack.pop()\n            cur=tmp.left\n        return res[::-1]<\/code><\/pre>\n<h5>\u5c42\u5e8f\u904d\u5386<\/h5>\n<p>\u5c42\u5e8f\u904d\u5386\u4f7f\u7528\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u6b65\u9aa4\u4e3a\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u961f\u5217<code>q<\/code>\uff0c\u5e76\u5c06\u6839\u8282\u70b9<code>root<\/code>\u52a0\u5165\u5230\u961f\u5217\u4e2d<\/li>\n<li>\u5f53\u961f\u5217\u4e0d\u4e3a\u7a7a\u65f6\uff1a\n<ul>\n<li>\u8ba1\u7b97\u5f53\u524d\u5c42\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u5373\u961f\u5217\u957f\u5ea6<\/li>\n<li>\u961f\u5217\u5f39\u51fa\u8282\u70b9<code>node<\/code>,\u52a0\u5165\u5230\u7ed3\u679c\u4e2d\uff1b<\/li>\n<li>\u5982\u679c\u5de6\u5b50\u6811\u975e\u7a7a\uff0c\u5de6\u5b50\u6811\u52a0\u5165\u961f\u5217\uff1b<\/li>\n<li>\u5982\u679c\u53f3\u5b50\u6811\u975e\u7a7a\uff0c\u53f3\u5b50\u6811\u52a0\u5165\u961f\u5217\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-python\">class Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -&gt; List[List[int]]:\n        if not root:\n            return []\n\n        queue=deque()\n        queue.append(root)\n\n        result=[]\n        while queue:\n            tmp=[]\n            num=len(queue)\n            for i in range(num):\n                node=queue.popleft()\n                tmp.append(node.val)\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n            result.append(tmp)\n\n        return result<\/code><\/pre>\n<h3>Leecode.617 \u5408\u5e76\u4e8c\u53c9\u6811<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/merge-two-binary-trees\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/merge-two-binary-trees\/description\/<\/a><\/p>\n<p>\u7ed9\u4f60\u4e24\u68f5\u4e8c\u53c9\u6811\uff1a <code>root1<\/code> \u548c <code>root2<\/code> \u3002<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u5f53\u4f60\u5c06\u5176\u4e2d\u4e00\u68f5\u8986\u76d6\u5230\u53e6\u4e00\u68f5\u4e4b\u4e0a\u65f6\uff0c\u4e24\u68f5\u6811\u4e0a\u7684\u4e00\u4e9b\u8282\u70b9\u5c06\u4f1a\u91cd\u53e0\uff08\u800c\u53e6\u4e00\u4e9b\u4e0d\u4f1a\uff09\u3002\u4f60\u9700\u8981\u5c06\u8fd9\u4e24\u68f5\u6811\u5408\u5e76\u6210\u4e00\u68f5\u65b0\u4e8c\u53c9\u6811\u3002\u5408\u5e76\u7684\u89c4\u5219\u662f\uff1a\u5982\u679c\u4e24\u4e2a\u8282\u70b9\u91cd\u53e0\uff0c\u90a3\u4e48\u5c06\u8fd9\u4e24\u4e2a\u8282\u70b9\u7684\u503c\u76f8\u52a0\u4f5c\u4e3a\u5408\u5e76\u540e\u8282\u70b9\u7684\u65b0\u503c\uff1b\u5426\u5219\uff0c\u4e0d\u4e3a<code>null<\/code>\u7684\u8282\u70b9\u5c06\u76f4\u63a5\u4f5c\u4e3a\u65b0\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u3002<\/p>\n<p>\u8fd4\u56de\u5408\u5e76\u540e\u7684\u4e8c\u53c9\u6811\u3002<\/p>\n<p><strong>\u6ce8\u610f:<\/strong> \u5408\u5e76\u8fc7\u7a0b\u5fc5\u987b\u4ece\u4e24\u4e2a\u6811\u7684\u6839\u8282\u70b9\u5f00\u59cb\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/merge.jpg\" alt=\"\" \/><\/p>\n<pre><code>\u8f93\u5165\uff1aroot1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]\n\u8f93\u51fa\uff1a[3,4,5,5,4,null,7]<\/code><\/pre>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<h5>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22<\/h5>\n<p>\u4f7f\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u904d\u5386\u5e76\u5408\u5e76\u4e24\u4e2a\u4e8c\u53c9\u6811\uff0c\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u4e8c\u53c9\u6811\uff0c\u5c06\u5bf9\u5e94\u7684\u8282\u70b9\u8fdb\u884c\u76f8\u52a0\u5408\u5e76\u3002<\/p>\n<p>\u5728\u904d\u5386\u4e8c\u53c9\u6811\u7684\u5404\u4e2a\u8282\u70b9\u65f6\uff0c\u4e00\u5171\u4f1a\u51fa\u73b0\u5982\u4e0b\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u90fd\u4e3a\u7a7a\uff0c\u5219\u5408\u5e76\u540e\u7684\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u4e5f\u4e3a\u7a7a\uff1b<\/li>\n<li>\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u53ea\u6709\u4e00\u4e2a\u4e3a\u7a7a\uff0c\u5219\u5408\u5e76\u540e\u7684\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u4e3a\u5176\u4e2d\u7684\u975e\u7a7a\u8282\u70b9\uff1b<\/li>\n<li>\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u90fd\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5408\u5e76\u540e\u7684\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u7684\u503c\u4e3a\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u5bf9\u5e94\u8282\u70b9\u7684\u503c\u4e4b\u548c\uff0c\u6b64\u65f6\u9700\u8981\u663e\u6027\u5408\u5e76\u4e24\u4e2a\u8282\u70b9\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e00\u4e2a\u8282\u70b9\u5408\u5e76\u5b8c\u6210\u540e\uff0c\u8fd8\u9700\u8981\u5bf9\u8be5\u8282\u70b9\u7684\u5de6\u53f3\u5b57\u6570\u8fdb\u884c\u76f8\u540c\u64cd\u4f5c\uff0c\u76f4\u81f3\u4e8c\u53c9\u6811\u4e3a\u7a7a\uff0c\u8be5\u8fc7\u7a0b\u662f\u4e00\u4e2a\u9012\u5f52\u8fc7\u7a0b\u3002<\/p>\n<h5>\u590d\u6742\u5ea6\u5206\u6790<\/h5>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(min(m,n))\uff0c\u5176\u4e2dm\u548cn\u5206\u522b\u662f\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u4e2a\u6570\u3002<br \/>\n<strong>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a<\/strong>O(min(m,n))\uff0c\u5176\u4e2dm\u548cn\u5206\u522b\u662f\u4e24\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u4e2a\u6570\u3002<\/p>\n<h5>\u6848\u4f8b\u4ee3\u7801<\/h5>\n<pre><code class=\"language-python\">class Solution:\n    def mergeTrees(self, t1: TreeNode, t2: TreeNode) -&gt; TreeNode:\n        #\u5982\u679c\u6709\u7a7a\u8282\u70b9\uff1a\n        if not t1:\n            return t2\n        if not t2:\n            return t1\n\n        #\u5982\u679c\u4e24\u9897\u6811\u7684\u8282\u70b9\u5747\u4e0d\u4e3a\u7a7a\uff0c\u5bf9\u4e24\u8282\u70b9\u503c\u8fdb\u884c\u5408\u5e76\n        Node=TreeNode(t1.val+t2.val)\n        #\u9012\u5f52\uff0c\u904d\u5386\u5de6\u53f3\u8282\u70b9\n        Node.left=mergeTrees(t1.left,t2.left)\n        Node.right=mergeTrees(t1.right,t2.right)\n        return Node<\/code><\/pre>\n<h3>Leecode.823 \u5e26\u56e0\u5b50\u7684\u4e8c\u53c9\u6811<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/binary-trees-with-factors\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/binary-trees-with-factors\/description\/<\/a><\/p>\n<p>\u7ed9\u51fa\u4e00\u4e2a\u542b\u6709\u4e0d\u91cd\u590d\u6574\u6570\u5143\u7d20\u7684\u6570\u7ec4 <code>arr<\/code>\uff0c\u6bcf\u4e2a\u6574\u6570<code>arr[i]<\/code>\u5747\u5927\u4e8e 1\u3002<\/p>\n<p>\u7528\u8fd9\u4e9b\u6574\u6570\u6765\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u6bcf\u4e2a\u6574\u6570\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6b21\u6570\u3002\u5176\u4e2d\uff1a\u6bcf\u4e2a\u975e\u53f6\u7ed3\u70b9\u7684\u503c\u5e94\u7b49\u4e8e\u5b83\u7684\u4e24\u4e2a\u5b50\u7ed3\u70b9\u7684\u503c\u7684\u4e58\u79ef\u3002<\/p>\n<p>\u6ee1\u8db3\u6761\u4ef6\u7684\u4e8c\u53c9\u6811\u4e00\u5171\u6709\u591a\u5c11\u4e2a\uff1f\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u8fd4\u56de \u5bf9<code>10^9 + 7<\/code>\u53d6\u4f59\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<blockquote>\n<p>\u8f93\u5165\uff1aarr=[2,4]<br \/>\n\u8f93\u51fa\uff1a3<br \/>\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e9b\u4e8c\u53c9\u6811\uff1a<code>[2],[4],[4,2,2]<\/code><\/p>\n<\/blockquote>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<p>\u52a8\u6001\u89c4\u5212+\u53cc\u6307\u9488<br \/>\n\u9898\u76ee\u4e2d\u6bcf\u4e2a\u6574\u6570arr[i]\u90fd\u5927\u4e8e1\uff0c\u56e0\u6b64\u6bcf\u4e2a\u975e\u5b50\u53f6\u8282\u70b9\u7684\u503c\u90fd\u5927\u4e8e\u5b83\u5b50\u53f6\u8282\u70b9\u7684\u503c\u3002<\/p>\n<ul>\n<li>\u5c06arr\u4ece\u5c0f\u5230\u5927\u8fdb\u884c\u6392\u5e8f\uff0c\u5bf9\u4e8e\u4ee5arr[i]\u4e3a\u6839\u8282\u70b9\u7684\u5e26\u56e0\u5b50\u7684\u4e8c\u53c9\u6811\uff0c\u5b83\u7684\u5b50\u5b59\u8282\u70b9\u503c\u5f97\u4e0b\u6807\u53ea\u80fd\u5728\u533a\u95f4[0,i-1]\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528dp[i]\u4fdd\u5b58\u4ee5arr[i]\u4e3a\u6839\u8282\u70b9\u7684\u5e26\u56e0\u5b50\u7684\u4e8c\u53c9\u6811\u6570\u76ee<\/li>\n<li>\u4ece\u533a\u95f4[0,i-1]\u5185\u679a\u4e3earr[i]\u7684\u5b50\u8282\u70b9\uff0c\u5047\u8bbe\u5b58\u57280&lt;=left&lt;=right\\&lt;i\uff0c\u4f7f\u5f97arr[left]*arr[right]=arr[i]\u6210\u7acb\uff0c\u90a3\u4e48arr[left]\u548carr[right]\u53ef\u4ee5\u4f5c\u4e3aarr[i]\u7684\u4e24\u4e2a\u5b50\u8282\u70b9\u3002<\/li>\n<li>arr[left]\u548carr[right]\u4e3a\u6839\u8282\u70b9\u7684\u5e26\u56e0\u5b50\u4e8c\u53c9\u6811\u6570\u76ee\u5206\u522b\u4e3adp[left]\u548cdp[right]<\/li>\n<li>arr[left]\u548carr[right]\u4f5c\u4e3aarr[i]\u7684\u4e24\u4e2a\u5b50\u8282\u70b9\u65f6\uff0c\u5e26\u56e0\u5b50\u4e8c\u53c9\u6811\u6570\u76eeS\u4e3a\uff1a\n<ul>\n<li>left=right\u65f6\uff0cs=dp[left]*dp[right]<\/li>\n<li>left!=right\u65f6\uff0c\u56e0\u4e3a\u4e24\u4e2a\u5b50\u8282\u70b9\u53ef\u4ee5\u4ea4\u6362\uff0c\u6240\u4ee5s=dp[left]<em>dp[right]<\/em>2<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5f53arr[i]\u6ca1\u6709\u5b50\u8282\u70b9\u65f6\uff0c\u5bf9\u5e941\u4e2a\u5e26\u56e0\u5b50\u4e8c\u53c9\u6811\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a<\/p>\n<p>$$dp[i]=1+\\sum_{(left,right)}dp[left]*dp[right]*(1+f(left,right))$$<\/p>\n<p>\u5176\u4e2d\uff0c<span class=\"katex-eq\" data-katex-display=\"false\">left=right<\/span>\u65f6\uff0c<span class=\"katex-eq\" data-katex-display=\"false\">f(left,right)=0<\/span>\uff0c<span class=\"katex-eq\" data-katex-display=\"false\">left \\neq right<\/span>\u65f6\uff0c<span class=\"katex-eq\" data-katex-display=\"false\">f(left,right)=1<\/span><\/p>\n<h4>\u590d\u6742\u5ea6\u5206\u6790<\/h4>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO\uff08n^2\uff09<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1ao\uff08n\uff09<\/li>\n<\/ul>\n<h4>\u6848\u4f8b\u4ee3\u7801<\/h4>\n<pre><code class=\"language-python\">class Solution:\n    def numFactoredBinaryTrees(self, arr: List[int]) -&gt; int:\n        n=len(arr)\n        arr=sorted(arr)\n\n        dp=[1]*n\n        res,mod=0,10**9+7\n\n        for i in range(n):\n            left,right=0,i-1\n\n            while left&lt;=right:\n                while right&gt;=left and arr[left]*arr[right]&gt;arr[i]:\n                    right-=1\n                if right&gt;=left and arr[left]*arr[right]==arr[i]:\n                    #\u5982\u679c\u4e24\u4e2a\u8282\u70b9\u4e0d\u76f8\u7b49\uff0c\u53ef\u4ee5\u5de6\u53f3\u4ea4\u6362\uff0c\u56e0\u6b64\u4e8c\u53c9\u6811\u6570\u76ee*2\n                    if right!=left:\n                        dp[i]=(dp[i]+dp[left]*dp[right]*2)%mod\n                    #\u76f8\u7b49\u65f6\u4e0d\u80fd\u4ea4\u6362\n                    else:\n                        dp[i]=(dp[i]+dp[left]*dp[right])%mod\n                left+=1\n            res=(res+dp[i])%mod\n        return res<\/code><\/pre>\n<h3>\u4e8c\u53c9\u6811\u7684\u6700\u5927\u6df1\u5ea6<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/er-cha-shu-de-shen-du-lcof\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/er-cha-shu-de-shen-du-lcof\/description\/<\/a><br \/>\n\u67d0\u516c\u53f8\u67b6\u6784\u4ee5\u4e8c\u53c9\u6811\u5f62\u5f0f\u8bb0\u5f55\uff0c\u8bf7\u8fd4\u56de\u8be5\u516c\u53f8\u7684\u5c42\u7ea7\u6570\u3002<\/p>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<p>\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\uff0c\u904d\u5386\u65f6\u5bf9\u6bcf\u4e00\u5c42\u8ba1\u6570<\/p>\n<h4>\u6848\u4f8b\u4ee3\u7801<\/h4>\n<pre><code class=\"language-python\"># Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def calculateDepth(self, root: Optional[TreeNode]) -&gt; int:\n        if not root:\n            return 0\n        queue=[]\n\n        queue.append(root)\n\n        count=0\n        while queue:\n            temp=queue\n            queue=[]\n            count+=1\n            for i in temp:\n                if i.left:\n                    queue.append(i.left)\n                if i.right:\n                    queue.append(i.right)\n\n        return count<\/code><\/pre>\n<h3>Leecode 589. N\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/n-ary-tree-preorder-traversal\/description\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/n-ary-tree-preorder-traversal\/description\/<\/a><br \/>\n\u7ed9\u5b9a\u4e00\u4e2an\u53c9\u6811\u7684\u6839\u8282\u70b9<code>root<\/code>,\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684\u524d\u5e8f\u904d\u5386<br \/>\nn\u53c9\u6811\u5728\u8f93\u5165\u4e2d\u6309\u5c42\u5e8f\u904d\u5386\u8fdb\u884c\u5e8f\u5217\u5316\u8868\u793a\uff0c\u6bcf\u7ec4\u5b50\u8282\u70b9\u7531\u7a7a\u503c<code>null<\/code>\u5206\u9694<\/p>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<p>\u4e0e\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u591a\u53c9\u6811\u4e2d\uff0c<code>left<\/code>\u8282\u70b9\u548c<code>right<\/code>\u8282\u70b9\u88ab\u591a\u4e2a<code>child<\/code>\u66ff\u4ee3\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<h4>\u6848\u4f8b\u4ee3\u7801<\/h4>\n<pre><code class=\"language-python\">class Solution:\n    def preorder(self, root: &#039;Node&#039;) -&gt; List[int]:\n        ans = []\n        def dfs(node: &#039;Node&#039;):\n            if node is None:\n                return\n            ans.append(node.val)\n            for ch in node.children:\n                dfs(ch)\n        dfs(root)\n        return ans<\/code><\/pre>\n<h3>Leecode 114. \u4e8c\u53c9\u6811\u5c55\u5f00\u4e3a\u94fe\u8868<\/h3>\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/flatten-binary-tree-to-linked-list\/description\/?envType=study-plan-v2&amp;envId=top-interview-150\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/leetcode.cn\/problems\/flatten-binary-tree-to-linked-list\/description\/?envType=study-plan-v2&envId=top-interview-150<\/a><br \/>\n\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9<code>root<\/code>\uff0c\u5c06\u4ed6\u5c55\u5f00\u4e3a\u4e00\u4e2a\u5355\u94fe\u8868\uff1a<br \/>\n\u5c55\u5f00\u540e\u7684\u5355\u94fe\u8868\u5e94\u8be5\u540c\u6837\u4f7f\u7528<code>TreeNode<\/code>\uff0c\u5176\u4e2d<code>right<\/code>\u5b50\u6307\u9488\u6307\u5411\u94fe\u8868\u4e2d\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff0c\u800c\u5de6\u5b50\u6307\u9488\u59cb\u7ec8\u4e3a<code>null<\/code>\u3002<br \/>\n\u5c55\u5f00\u540e\u7684\u5355\u94fe\u8868\u5e94\u8be5\u4e0e\u4e8c\u53c9\u6811<strong>\u5148\u5e8f\u904d\u5386<\/strong>\u987a\u5e8f\u76f8\u540c\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/08\/flaten.jpg\" alt=\"\" \/><\/p>\n<h4>\u89e3\u9898\u601d\u8def<\/h4>\n<p>\u5c06\u4e8c\u53c9\u6811\u5c55\u5f00\u4e3a\u5355\u94fe\u8868\u4e4b\u540e\uff0c\u5355\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u987a\u5e8f\u51e0\u4f4d\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u8bbf\u95ee\u5404\u8282\u70b9\u7684\u987a\u5e8f\uff0c\u56e0\u6b64\uff0c\u53ef\u4ee5\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u524d\u5e8f\u904d\u5386\uff0c\u83b7\u5f97\u5404\u8282\u70b9\u88ab\u8bbf\u95ee\u5230\u7684\u987a\u5e8f\u3002<\/p>\n<h4>\u6848\u4f8b\u4ee3\u7801<\/h4>\n<pre><code class=\"language-python\">class Solution:\n    def flatten(self, root: Optional[TreeNode]) -&gt; None:\n        preorder_list=[]\n        def preorder(root):\n            if root:\n                preorder.append(root)\n                preorder(root.left)\n                preorder(root.right)\n\n        preorder(root)\n        size=len(preorder_list)\n        for i in range(1,size):\n            prev,curr=preorder_list[i-1],preorder_list[i]\n            prev.left=None\n            prev.right=curr<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u540e\u5e8f\u904d\u5386\u548c\u9012\u5f52\u5b8c\u6210\u89e3\u9898\uff1a<\/p>\n<pre><code class=\"language-python\">class Solution:\n    def flatten(self, root: Optional[TreeNode]) -&gt; None:\n        &quot;&quot;&quot;\n        Do not return anything, modify root in-place instead.\n        &quot;&quot;&quot;\n\n        def preorder(root):\n\n            if root == None:\n                return \n\n            preorder(root.left)\n            preorder(root.right)\n\n            if root.left!=None:\n                pre=root.left\n                while pre.right:\n                    pre=pre.right\n                pre.right=root.right\n                root.right=root.left\n                root.left=None\n            root = root.right\n\n        preorder(root)<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u6784\u9020\u56fe\u7684\u65b9\u6cd5\u89e3\u9898\uff1a<\/p>\n<pre><code class=\"language-python\"># Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def buildTree(self, inorder: List[int], postorder: List[int]) -&gt; Optional[TreeNode]:\n        # \u7ec8\u6b62\u9012\u5f52\u6761\u4ef6\uff0c\u6811\u4e3a\u7a7a\n        if not inorder or not postorder:\n            return None\n\n        #\u6839\u8282\u70b9\u7684\u503c\u4e3a\u540e\u5e8f\u904d\u5386\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u503c\n        rootVal = postorder[-1]\n\n        #\u521b\u5efa\u6839\u8282\u70b9\n        root = TreeNode(rootVal)\n\n        #\u7528\u6839\u8282\u70b9\u7684\u503c\u53bb\u4e2d\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\u4e0b\u6807\n        midIndex=inorder.index(rootVal)\n\n        #\u627e\u51fa\u4e2d\u5e8f\u904d\u5386\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\n        #\u5de6\u5b50\u6811\u533a\u95f4\u4e3a[0\uff0cmidIndex)\uff0c\u53f3\u5b50\u6811\u7684\u533a\u95f4\u4e3a[midIndex+1,n-1]\n        inorderLeft=inorder[:midIndex]\n        inorderRight=inorder[midIndex+1:]\n\n        #\u627e\u51fa\u540e\u5e8f\u904d\u5386\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\n        #\u540e\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u957f\u5ea6\u76f8\u7b49\uff0c\u6240\u4ee5\u53ef\u4ee5\u901a\u8fc7\u4e2d\u5e8f\u904d\u5386\u7684\u5de6\u53f3\u5b50\u6811\u957f\u5ea6\u6765\u786e\u5b9a\u540e\u5e8f\u904d\u5386\u5de6\u53f3\u5b50\u6811\u7684\u4f4d\u7f6e\n        postorderLeft = postorder[:len(inorderLeft)]\n        postorderRight=postorder[len(inorderLeft):len(inorder)-1]\n\n        # \u9012\u5f52\u904d\u5386\u5de6\u5b50\u6811\n        root.left=self.buildTree(inorderLeft,postorderLeft)\n        # \u9012\u5f52\u904d\u5386\u53f3\u5b50\u6811\n        root.right=self.buildTree(inorderRight,postorderRight)\n\n        return root<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u53c9\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u3001\u5c42\u5e8f\u904d\u5386 \u6709\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a\uff1a \u5219\u524d\u3001\u4e2d\u3001\u540e\u904d\u5386\u5bf9\u5e94\u7684\u89c4\u5219\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a \u524d\u5e8f\u904d\u5386\uff1a\u5148\u6839\u3001\u518d\u5de6\u3001\u518d\u53f3\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[23,2],"tags":[25,27],"class_list":["post-226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-leecode","category-algorithm_learning","tag-leecode","tag-27"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":14,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":716,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions\/716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media\/229"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}