{"id":1907,"date":"2016-03-24T15:55:08","date_gmt":"2016-03-24T07:55:08","guid":{"rendered":"http:\/\/boweihe.me\/?p=1907"},"modified":"2016-03-24T15:55:08","modified_gmt":"2016-03-24T07:55:08","slug":"leetcode-257-binary-tree-paths","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1907","title":{"rendered":"LeetCode 257. Binary Tree Paths"},"content":{"rendered":"<p>Given a binary tree, return all root-to-leaf paths.<br \/>\nFor example, given the following binary tree:<\/p>\n<pre>   1\n \/   \\\n2     3\n \\\n  5\n<\/pre>\n<p>All root-to-leaf paths are:<\/p>\n<pre>[\"1-&gt;2-&gt;5\", \"1-&gt;3\"]<\/pre>\n<p><b>\u4ee3\u7801<\/b><\/p>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    vector&lt;string&gt; binaryTreePaths(TreeNode* root) {\n        vector&lt;string&gt; results;\n        vector&lt;int&gt; path;\n        binaryTreePaths(root, path, results);\n        return results;\n    }\n    void binaryTreePaths(TreeNode* root, vector&lt;int&gt; path, vector&lt;string&gt;&amp; results){\n        if(root == NULL){\n            \/\/Error\n            return;\n        }\n        path.push_back(root-&gt;val);\n        if(root-&gt;left == NULL &amp;&amp; root-&gt;right == NULL) {\n            \/\/Leaf\n            string result;\n            int pSize = path.size();\n            for(int i=0; i&lt;pSize; i++){\n                result += to_string(path[i]);\n                if(i != pSize - 1)\n                    result += \"-&gt;\";\n            }\n            results.push_back(result);\n        } else {\n            if(root-&gt;left != NULL)\n                binaryTreePaths(root-&gt;left, path, results);\n            if(root-&gt;right != NULL)\n                binaryTreePaths(root-&gt;right, path, results);\n        }\n    }\n};<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 \/ \\ 2 3 \\ 5 All root-to-leaf paths are: [&#8220;1-&gt;2-&gt;5&#8221;, &#8220;1-&gt;3&#8221;] \u4ee3\u7801 \/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1907","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1907"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1907\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}