{"id":6165,"date":"2020-01-29T17:34:46","date_gmt":"2020-01-30T01:34:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6165"},"modified":"2020-01-29T17:34:53","modified_gmt":"2020-01-30T01:34:53","slug":"leetcode-144-binary-tree-preorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-144-binary-tree-preorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 144. Binary Tree Preorder Traversal"},"content":{"rendered":"\n<p>Given a binary tree, return the&nbsp;<em>preorder<\/em>&nbsp;traversal of its nodes&#8217; values.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>&nbsp;<code>[1,null,2,3]<\/code>\n   1\n    \\\n     2\n    \/\n   3\n\n<strong>Output:<\/strong>&nbsp;<code>[1,2,3]<\/code>\n<\/pre>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Recursive solution is trivial, could you do it iteratively?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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<int> preorderTraversal(TreeNode* root) {\n    vector<int> ans;    \n    function<void(TreeNode*)> preorder = [&](TreeNode* n) {\n      if (!n) return;\n      ans.push_back(n->val);\n      preorder(n->left);\n      preorder(n->right);\n    };\n    preorder(root);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Stack<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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<int> preorderTraversal(TreeNode* root) {\n    vector<int> ans;\n    stack<TreeNode*> s;\n    if (root) s.push(root);\n    while (!s.empty()) {\n      TreeNode* n = s.top();\n      ans.push_back(n->val);\n      s.pop();\n      if (n->right) s.push(n->right);\n      if (n->left) s.push(n->left);            \n    }\n    return ans;\n  }\n<\/pre>\n<\/div><\/div>\n\n\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return the&nbsp;preorder&nbsp;traversal of its nodes&#8217; values. Example: Input:&nbsp;[1,null,2,3] 1 \\ 2 \/ 3 Output:&nbsp;[1,2,3] Follow up:&nbsp;Recursive solution is trivial, could you&#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,541,376,17,180,32,28],"class_list":["post-6165","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-iteration","tag-on","tag-recursion","tag-stack","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6165","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=6165"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6165\/revisions"}],"predecessor-version":[{"id":6167,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6165\/revisions\/6167"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}