{"id":2768,"date":"2022-04-16T23:09:10","date_gmt":"2022-04-16T15:09:10","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2768"},"modified":"2022-04-16T23:09:10","modified_gmt":"2022-04-16T15:09:10","slug":"%e7%ae%97%e6%b3%95%e5%9f%ba%e7%a1%80%e7%ac%ac%e4%b8%83%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2768","title":{"rendered":"\u7b97\u6cd5\u57fa\u7840\u7b2c\u4e03\u5929"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/populating-next-right-pointers-in-each-node-ii\/\">117. \u586b\u5145\u6bcf\u4e2a\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u6307\u9488 II<\/a><\/p>\n\n\n\n<p>\u8fd9\u662f116\u9898\u7684\u666e\u904d\u89e3\u6cd5\uff0c\u9002\u7528\u4e8e116\uff0c116\u662f\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\uff0c\u8fd9\u4e2a\u662f\u666e\u901a\u7684\u4e8c\u53c9\u6811<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> 117. \u586b\u5145\u6bcf\u4e2a\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u6307\u9488 II<br><\/em><em> \u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811<br><\/em><em><br><\/em><em> struct Node {<br><\/em><em> int val;<br><\/em><em> Node *left;<br><\/em><em> Node *right;<br><\/em><em> Node *next;<br><\/em><em> }<br><\/em><em> \u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u8ba9\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u3002\u5982\u679c\u627e\u4e0d\u5230\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5219\u5c06 next \u6307\u9488\u8bbe\u7f6e\u4e3a NULL\u3002<br><\/em><em><br><\/em><em> \u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709 next \u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a NULL\u3002<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u8fdb\u9636\uff1a<br><\/em><em><br><\/em><em> \u4f60\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u7ea7\u989d\u5916\u7a7a\u95f4\u3002<br><\/em><em> \u4f7f\u7528\u9012\u5f52\u89e3\u9898\u4e5f\u7b26\u5408\u8981\u6c42\uff0c\u672c\u9898\u4e2d\u9012\u5f52\u7a0b\u5e8f\u5360\u7528\u7684\u6808\u7a7a\u95f4\u4e0d\u7b97\u505a\u989d\u5916\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002<br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b\uff1a<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u8f93\u5165\uff1aroot = [1,2,3,4,5,null,7]<br><\/em><em> \u8f93\u51fa\uff1a[1,#,2,3,#,4,5,7,#]<br><\/em><em> \u89e3\u91ca\uff1a\u7ed9\u5b9a\u4e8c\u53c9\u6811\u5982\u56fe A \u6240\u793a\uff0c\u4f60\u7684\u51fd\u6570\u5e94\u8be5\u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u4ee5\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5982\u56fe B \u6240\u793a\u3002\u5e8f\u5217\u5316\u8f93\u51fa\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\uff08\u7531 next \u6307\u9488\u8fde\u63a5\uff09\uff0c'#' \u8868\u793a\u6bcf\u5c42\u7684\u672b\u5c3e\u3002<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> \u6811\u4e2d\u7684\u8282\u70b9\u6570\u5c0f\u4e8e 6000<br><\/em><em> -100 &lt;= node.val &lt;= 100<br><\/em><em><br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question117 {<br><br>    public Node connect(Node root) {<br><br>        \/\/\u76f8\u6bd4116\u6765\u8bf4\uff0c\u8fd9\u662f\u666e\u904d\u60c5\u51b5\uff0c116\u662f\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\uff0c116. \u586b\u5145\u6bcf\u4e2a\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u6307\u9488<br><br>        if(root == null) return root;<br>        Node cur = root;<br>        while (cur!=null){<br>            Node dummy = new Node(0);<br>            Node pre = dummy;<br><br>            while (cur != null) {<br>                \/\/ \u4e00\u5c42\u4e2d\u7684\u6bcf\u4e00\u4e2a<br>                if (cur.left != null) {<br>                    pre.next = cur.left;<br>                    pre = pre.next;<br>                }<br><br>                if (cur.right != null) {<br>                    pre.next = cur.right;<br>                    pre = pre.next;<br>                }<br>                cur = cur.next;<br>            }<br><br>            cur = dummy.next; \/\/ \u4e0b\u4e00\u5c42\u7684\u7b2c\u4e00\u4e2a<br>        }<br><br>        return root;<br>    }<br><br>}<\/pre>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/subtree-of-another-tree\/\">572. \u53e6\u4e00\u68f5\u6811\u7684\u5b50\u6811<\/a><\/p>\n\n\n\n<p>\u8fd9\u9898\u53ef\u4ee5\u63d0\u53d6\u51fa\u6765\u4e00\u4e2a\u540c\u4e00\u4e2a\u6811\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> 572. \u53e6\u4e00\u68f5\u6811\u7684\u5b50\u6811<br><\/em><em> \u7ed9\u4f60\u4e24\u68f5\u4e8c\u53c9\u6811 root \u548c subRoot \u3002\u68c0\u9a8c root \u4e2d\u662f\u5426\u5305\u542b\u548c subRoot \u5177\u6709\u76f8\u540c\u7ed3\u6784\u548c\u8282\u70b9\u503c\u7684\u5b50\u6811\u3002\u5982\u679c\u5b58\u5728\uff0c\u8fd4\u56de true \uff1b\u5426\u5219\uff0c\u8fd4\u56de false \u3002<br><\/em><em><br><\/em><em> \u4e8c\u53c9\u6811 tree \u7684\u4e00\u68f5\u5b50\u6811\u5305\u62ec tree \u7684\u67d0\u4e2a\u8282\u70b9\u548c\u8fd9\u4e2a\u8282\u70b9\u7684\u6240\u6709\u540e\u4ee3\u8282\u70b9\u3002tree \u4e5f\u53ef\u4ee5\u770b\u505a\u5b83\u81ea\u8eab\u7684\u4e00\u68f5\u5b50\u6811\u3002<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em><br><\/em><em> \u8f93\u5165\uff1aroot = [3,4,5,1,2], subRoot = [4,1,2]<br><\/em><em> \u8f93\u51fa\uff1atrue<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em><br><\/em><em> \u8f93\u5165\uff1aroot = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]<br><\/em><em> \u8f93\u51fa\uff1afalse<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> root \u6811\u4e0a\u7684\u8282\u70b9\u6570\u91cf\u8303\u56f4\u662f [1, 2000]<br><\/em><em> subRoot \u6811\u4e0a\u7684\u8282\u70b9\u6570\u91cf\u8303\u56f4\u662f [1, 1000]<br><\/em><em> -10^4 &lt;= root.val &lt;= 10^4<br><\/em><em> -10^4 &lt;= subRoot.val &lt;= 10^4<br><\/em><em><br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question572 {<br><br>    public boolean isSubtree(TreeNode root, TreeNode subRoot) {<br>        if(root == null &amp;&amp; subRoot == null) return true;<br>        if(root == null || subRoot == null) return false;<br><br>        return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot) || isSameTree(root, subRoot);<br>    }<br><br><br>    public boolean isSameTree(TreeNode root, TreeNode subRoot) {<br>        if(root==null &amp;&amp; subRoot==null)return true;<br>        if(root==null || subRoot==null)return false;<br><br>        if(root.val != subRoot.val) return false;<br><br>        return isSameTree(root.left, subRoot.left) &amp;&amp; isSameTree(root.right, subRoot.right);<br>    }<br><br><br><br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>117. \u586b\u5145\u6bcf\u4e2a\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u6307\u9488 II \u8fd9\u662f116\u9898\u7684\u666e\u904d\u89e3\u6cd5\uff0c\u9002\u7528\u4e8e116\uff0c116\u662f\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2768"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2768"}],"version-history":[{"count":5,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2768\/revisions"}],"predecessor-version":[{"id":2773,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2768\/revisions\/2773"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}