{"id":744,"date":"2020-06-29T23:58:41","date_gmt":"2020-06-29T15:58:41","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=744"},"modified":"2020-06-29T23:58:41","modified_gmt":"2020-06-29T15:58:41","slug":"jz24-%e4%ba%8c%e5%8f%89%e6%a0%91%e4%b8%ad%e5%92%8c%e4%b8%ba%e6%9f%90%e4%b8%80%e5%80%bc%e7%9a%84%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=744","title":{"rendered":"JZ24 \u2014 \u4e8c\u53c9\u6811\u4e2d\u548c\u4e3a\u67d0\u4e00\u503c\u7684\u8def\u5f84"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u8f93\u5165\u4e00\u9897\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u548c\u4e00\u4e2a\u6574\u6570\uff0c\u6309\u5b57\u5178\u5e8f\u6253\u5370\u51fa\u4e8c\u53c9\u6811\u4e2d\u7ed3\u70b9\u503c\u7684\u548c\u4e3a\u8f93\u5165\u6574\u6570\u7684\u6240\u6709\u8def\u5f84\u3002\u8def\u5f84\u5b9a\u4e49\u4e3a\u4ece\u6811\u7684\u6839\u7ed3\u70b9\u5f00\u59cb\u5f80\u4e0b\u4e00\u76f4\u5230\u53f6\u7ed3\u70b9\u6240\u7ecf\u8fc7\u7684\u7ed3\u70b9\u5f62\u6210\u4e00\u6761\u8def\u5f84\u3002<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u5373\u53ef\u3002<\/p>\n<h3>C++<\/h3>\n<pre><code class=\"language-cpp line-numbers\">\/*\nstruct TreeNode {\n    int val;\n    struct TreeNode *left;\n    struct TreeNode *right;\n    TreeNode(int x) :\n            val(x), left(NULL), right(NULL) {\n    }\n};*\/\n\nclass Solution\n{\npublic:\n    vector&lt;vector&lt;int&gt; &gt; FindPath(TreeNode *root, int expectNumber)\n    {\n        vector&lt;vector&lt;int&gt; &gt; ans;\n        if (!root)\n            return ans;\n        FindPath2(root, expectNumber, vector&lt;int&gt;(), ans);\n        return ans;\n    }\n\n    void FindPath2(TreeNode *root, int expectNumber, vector&lt;int&gt; v, vector&lt;vector&lt;int&gt; &gt; &amp;ans)\n    {\n        v.push_back(root-&gt;val);\n        if (!root-&gt;left &amp;&amp; !root-&gt;right)\n        {\n            if (root-&gt;val == expectNumber)\n                ans.push_back(v);\n        }\n        else\n        {\n            vector&lt;int&gt; ans1, ans2;\n            if (root-&gt;left)\n                FindPath2(root-&gt;left, expectNumber - root-&gt;val, v, ans);\n\n            if (root-&gt;right)\n                FindPath2(root-&gt;right, expectNumber - root-&gt;val, v, ans);\n        }\n    }\n};\n<\/code><\/pre>\n<h3>Java<\/h3>\n<pre><code class=\"language-java line-numbers\">import java.util.*;\n\/**\npublic class TreeNode {\n    int val = 0;\n    TreeNode left = null;\n    TreeNode right = null;\n\n    public TreeNode(int val) {\n        this.val = val;\n\n    }\n\n}\n*\/\npublic class Solution {\n    public ArrayList&lt;ArrayList&lt;Integer&gt;&gt; FindPath(TreeNode root, int target) {\n        ArrayList&lt;ArrayList&lt;Integer&gt;&gt; ans = new ArrayList&lt;&gt;();\n        if (root == null)\n            return ans;\n        FindPath2(root, target, new ArrayList&lt;&gt;(), ans);\n        return ans;\n    }\n\n    private void FindPath2(TreeNode root, int target, ArrayList&lt;Integer&gt; list, ArrayList&lt;ArrayList&lt;Integer&gt;&gt; ans) {\n        list.add(root.val);\n        if (root.left == null &amp;&amp; root.right == null &amp;&amp; root.val == target) {\n            ans.add(list);\n        } else {\n            if (root.left != null)\n                FindPath2(root.left, target - root.val, new ArrayList&lt;&gt;(list), ans);\n            if (root.right != null)\n                FindPath2(root.right, target - root.val, new ArrayList&lt;&gt;(list), ans);\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u8f93\u5165\u4e00\u9897\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u548c\u4e00\u4e2a\u6574\u6570\uff0c\u6309\u5b57\u5178\u5e8f\u6253\u5370\u51fa\u4e8c\u53c9\u6811\u4e2d\u7ed3\u70b9\u503c\u7684\u548c\u4e3a\u8f93\u5165\u6574\u6570\u7684\u6240\u6709\u8def\u5f84\u3002\u8def\u5f84\u5b9a\u4e49\u4e3a\u4ece [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[33,39],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-algo","tag-33","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=744"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}