{"id":2318,"date":"2018-03-23T01:08:59","date_gmt":"2018-03-23T08:08:59","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2318"},"modified":"2018-03-23T01:21:27","modified_gmt":"2018-03-23T08:21:27","slug":"leetcode-94-binary-tree-inorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-94-binary-tree-inorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 94. Binary Tree Inorder Traversal"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a binary tree, return the\u00a0<i>inorder<\/i>\u00a0traversal of its nodes&#8217; values.<\/p>\n<p>For example:<br \/>\nGiven binary tree\u00a0<code>[1,null,2,3]<\/code>,<\/p>\n<pre class=\"crayon:false \">   1\r\n    \\\r\n     2\r\n    \/\r\n   3\r\n<\/pre>\n<p>return\u00a0<code>[1,3,2]<\/code>.<\/p>\n<p><b>Note:<\/b>\u00a0Recursive solution is trivial, could you do it iteratively?<\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(h)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; inorderTraversal(TreeNode* root) {    \r\n    vector&lt;int&gt; ans;\r\n    inorderTraversal(root, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void inorderTraversal(TreeNode* root, vector&lt;int&gt;&amp; ans) {\r\n    if (root == nullptr) return;\r\n    inorderTraversal(root-&gt;left, ans);\r\n    ans.push_back(root-&gt;val);\r\n    inorderTraversal(root-&gt;right, ans);    \r\n  }\r\n};<\/pre>\n<h1><strong>Solution 2: Iterative<\/strong><\/h1>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nlass Solution {\r\npublic:\r\n  vector&lt;int&gt; inorderTraversal(TreeNode* root) {\r\n    if (root == nullptr) return {};\r\n    vector&lt;int&gt; ans;\r\n    stack&lt;TreeNode*&gt; s;\r\n    TreeNode* curr = root;\r\n    while (curr != nullptr || !s.empty()) {\r\n      while (curr != nullptr) {\r\n        s.push(curr);\r\n        curr = curr-&gt;left;\r\n      }\r\n      curr = s.top(); s.pop();\r\n      ans.push_back(curr-&gt;val);\r\n      curr = curr-&gt;right;\r\n    }    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary tree, return the\u00a0inorder\u00a0traversal of its nodes&#8217; values. For example: Given binary tree\u00a0[1,null,2,3], 1 \\ 2 \/ 3 return\u00a0[1,3,2]. Note:\u00a0Recursive solution is&#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,54,17],"class_list":["post-2318","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-iterative","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2318","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=2318"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":2321,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2318\/revisions\/2321"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}