{"id":1978,"date":"2018-03-05T20:55:25","date_gmt":"2018-03-06T04:55:25","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1978"},"modified":"2018-03-05T21:02:01","modified_gmt":"2018-03-06T05:02:01","slug":"leetcode-513-find-bottom-left-tree-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-513-find-bottom-left-tree-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 513. Find Bottom Left Tree Value"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u6811\uff0c\u8fd4\u56de\u5de6\u4e0b\u89d2\uff08\u6700\u540e\u4e00\u884c\u6700\u5de6\u9762\uff09\u7684\u8282\u70b9\u7684\u503c\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/find-bottom-left-tree-value\/description\/\">https:\/\/leetcode.com\/problems\/find-bottom-left-tree-value\/description\/<\/a><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a binary tree, find the leftmost value in the last row of the tree.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre>Input:\r\n\r\n    2\r\n   \/ \\\r\n  1   3\r\n\r\nOutput:\r\n1\r\n<\/pre>\n<p><b>Example 2:\u00a0<\/b><\/p>\n<pre>Input:\r\n\r\n        1\r\n       \/ \\\r\n      2   3\r\n     \/   \/ \\\r\n    4   5   6\r\n       \/\r\n      7\r\n\r\nOutput:\r\n7\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0You may assume the tree (i.e., the given root node) is not\u00a0<b>NULL<\/b>.<\/p>\n<p><strong>Solution 1: Inorder traversal with depth info<\/strong><\/p>\n<p>The first node visited in the deepest row is the answer.<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  int findBottomLeftValue(TreeNode* root) {\r\n    int max_row = -1;    \r\n    int ans;\r\n    dfs(root, 0, max_row, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void inorder(TreeNode* root, int row, int&amp; max_row, int&amp; ans) {\r\n    if (root == nullptr) return;\r\n    inorder(root-&gt;left, row + 1, max_row, ans);\r\n    if (row &gt; max_row) {\r\n      ans = root-&gt;val;\r\n      max_row = row;      \r\n    }\r\n    inorder(root-&gt;right, row + 1, max_row, ans);\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 92 ms\r\n\"\"\"\r\nclass Solution:\r\n  def inorder(self, root, row):\r\n    if not root: return\r\n    self.inorder(root.left, row + 1)\r\n    if row &gt; self.max_row:\r\n      self.max_row, self.ans = row, root.val\r\n    self.inorder(root.right, row + 1)    \r\n      \r\n  def findBottomLeftValue(self, root):        \r\n    self.ans, self.max_row = 0, -1\r\n    self.inorder(root, 0)\r\n    return self.ans<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u6811\uff0c\u8fd4\u56de\u5de6\u4e0b\u89d2\uff08\u6700\u540e\u4e00\u884c\u6700\u5de6\u9762\uff09\u7684\u8282\u70b9\u7684\u503c\u3002 https:\/\/leetcode.com\/problems\/find-bottom-left-tree-value\/description\/ Problem: Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 \/ \\ 1&#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":[33,58,177,32,28],"class_list":["post-1978","post","type-post","status-publish","format-standard","hentry","category-tree","tag-dfs","tag-inorder","tag-medium","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1978","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=1978"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions"}],"predecessor-version":[{"id":1981,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions\/1981"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}