{"id":8894,"date":"2021-11-28T19:43:18","date_gmt":"2021-11-29T03:43:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8894"},"modified":"2021-11-28T19:43:37","modified_gmt":"2021-11-29T03:43:37","slug":"leetcode-199-binary-tree-right-side-view","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-199-binary-tree-right-side-view\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 199. Binary Tree Right Side View"},"content":{"rendered":"\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, imagine yourself standing on the&nbsp;<strong>right side<\/strong>&nbsp;of it, return&nbsp;<em>the values of the nodes you can see ordered from top to bottom<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/14\/tree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,null,5,null,4]\n<strong>Output:<\/strong> [1,3,4]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,null,3]\n<strong>Output:<\/strong> [1,3]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = []\n<strong>Output:<\/strong> []\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the tree is in the range&nbsp;<code>[0, 100]<\/code>.<\/li><li><code>-100 &lt;= Node.val &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Pre-order traversal<\/strong><\/h2>\n\n\n\n<p>By using pre-order traversal, the right most node will be the last one to visit in each level.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(|height|)<\/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> rightSideView(TreeNode* root) {\n    vector<int> ans;\n    function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int d) {\n      if (!root) return;\n      if (ans.size() < d + 1) ans.push_back(0);\n      ans[d] = root->val;\n      dfs(root->left, d + 1);\n      dfs(root->right, d + 1);\n    };\n    dfs(root, 0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, imagine yourself standing on the&nbsp;right side&nbsp;of it, return&nbsp;the values of the nodes you can see ordered from top to bottom.&#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,329,736,28],"class_list":["post-8894","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-level","tag-pre-order","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8894","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=8894"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8894\/revisions"}],"predecessor-version":[{"id":8896,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8894\/revisions\/8896"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}