{"id":1982,"date":"2018-03-05T21:08:33","date_gmt":"2018-03-06T05:08:33","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1982"},"modified":"2018-03-05T21:12:45","modified_gmt":"2018-03-06T05:12:45","slug":"leetcode-515-find-largest-value-in-each-tree-row","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-515-find-largest-value-in-each-tree-row\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 515. Find Largest Value in Each Tree Row"},"content":{"rendered":"<div class=\"question-description\">\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u6811\uff0c\u8fd4\u56de\u6bcf\u4e00\u5c42\u6700\u5927\u7684\u5143\u7d20\u7684\u503c\u3002<\/p>\n<p>You need to find the largest value in each row of a binary tree.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\"><b>Input:<\/b> \r\n\r\n          1\r\n         \/ \\\r\n        3   2\r\n       \/ \\   \\  \r\n      5   3   9 \r\n\r\n<b>Output:<\/b> [1, 3, 9]\r\n<\/pre>\n<\/div>\n<p>Solution 1: Inorder traversal + depth info<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 15 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; largestValues(TreeNode* root) {\r\n    vector&lt;int&gt; ans;\r\n    inorder(root, 0, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void inorder(TreeNode* root, int d, vector&lt;int&gt;&amp; ans) {\r\n    if (root == nullptr) return;\r\n    while (ans.size() &lt;= d) ans.push_back(INT_MIN);    \r\n    inorder(root-&gt;left, d + 1, ans);\r\n    ans[d] = max(ans[d], root-&gt;val);\r\n    inorder(root-&gt;right, d + 1, ans);\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 72 ms (beats 100%)\r\n\"\"\"\r\nclass Solution:\r\n  def largestValues(self, root):\r\n    def inorder(root, d, ans):\r\n      if not root: return ans\r\n      if len(ans) &lt;= d: ans += [float('-inf')]\r\n      inorder(root.left, d + 1, ans)\r\n      if root.val &gt; ans[d]: ans[d] = root.val\r\n      inorder(root.right, d + 1, ans)\r\n      return ans\r\n    \r\n    return inorder(root, 0, [])<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u6811\uff0c\u8fd4\u56de\u6bcf\u4e00\u5c42\u6700\u5927\u7684\u5143\u7d20\u7684\u503c\u3002 You need to find the largest value in each row of a binary tree. Example: Input: 1 \/ \\ 3 2 \/ \\ \\&#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":[58,228,177,233,28],"class_list":["post-1982","post","type-post","status-publish","format-standard","hentry","category-tree","tag-inorder","tag-max-element","tag-medium","tag-travesal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1982","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=1982"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1982\/revisions"}],"predecessor-version":[{"id":1985,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1982\/revisions\/1985"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}