{"id":6171,"date":"2020-01-29T19:19:37","date_gmt":"2020-01-30T03:19:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6171"},"modified":"2020-01-29T19:20:46","modified_gmt":"2020-01-30T03:20:46","slug":"leetcode-257-binary-tree-paths","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-257-binary-tree-paths\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 257. Binary Tree Paths"},"content":{"rendered":"\n<p>Given a binary tree, return all root-to-leaf paths.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;A leaf is a node with no children.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\n\n   1\n \/   \\\n2     3\n \\\n  5\n\n<strong>Output:<\/strong> [\"1-&gt;2-&gt;5\", \"1-&gt;3\"]\n\n<strong>Explanation:<\/strong> All root-to-leaf paths are: 1-&gt;2-&gt;5, 1-&gt;3<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) \/ output can be O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> binaryTreePaths(TreeNode* root) {\n    vector<string> ans;\n    string s;\n    function<void(TreeNode*, int)> preorder = [&](TreeNode* node, int l) {\n      if (!node) return;\n      s += (l > 0 ? \"->\" : \"\") + to_string(node->val);\n      if (!node->left && !node->right)\n        ans.push_back(s);\n      preorder(node->left, s.size());\n      preorder(node->right, s.size());\n      while (s.size() != l) s.pop_back();\n    };\n    preorder(root, 0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return all root-to-leaf paths. Note:&nbsp;A leaf is a node with no children. Example: Input: 1 \/ \\ 2 3 \\ 5&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[222,376,116,17,28],"class_list":["post-6171","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-on","tag-path","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6171"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6171\/revisions"}],"predecessor-version":[{"id":6173,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6171\/revisions\/6173"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}