{"id":7060,"date":"2020-07-09T23:52:03","date_gmt":"2020-07-10T06:52:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7060"},"modified":"2020-07-10T18:07:55","modified_gmt":"2020-07-11T01:07:55","slug":"leetcode-662-maximum-width-of-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-662-maximum-width-of-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 662. Maximum Width of Binary Tree"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 662. Maximum Width of Binary Tree - \u5237\u9898\u627e\u5de5\u4f5c EP342\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/7LBdfdC8vhs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a&nbsp;<strong>full binary tree<\/strong>, but some nodes are null.<\/p>\n\n\n\n<p>The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the&nbsp;<code>null<\/code>&nbsp;nodes between the end-nodes are also counted into the length calculation.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \n\n           1\n         \/   \\\n        3     2\n       \/ \\     \\  \n      5   3     9 \n\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The maximum width existing in the third level with the length 4 (5,3,null,9).\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> \n\n          1\n         \/  \n        3    \n       \/ \\       \n      5   3     \n\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The maximum width existing in the third level with the length 2 (5,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> \n\n          1\n         \/ \\\n        3   2 \n       \/        \n      5      \n\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The maximum width existing in the second level with the length 2 (3,2).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \n\n          1\n         \/ \\\n        3   2\n       \/     \\  \n      5       9 \n     \/         \\\n    6           7\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-1.png\" alt=\"\" class=\"wp-image-7064\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-2.png\" alt=\"\" class=\"wp-image-7065\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/07\/662-ep342-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Let us assign an id to each node, similar to the index of a heap. root is 1, left child = parent * 2, right child = parent * 2 + 1. Width = id(right most child) &#8211; id(left most child) + 1, so far so good.<br>However, this kind of id system grows exponentially, it overflows even with long type with just 64 levels. To avoid that, we can remap the id with id &#8211; id(left most child of each level).<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/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  int widthOfBinaryTree(TreeNode* root) {\n    vector<int> ids; \/\/ left most id of each level.\n    function<int(TreeNode*, int, int)> dfs = [&](TreeNode* node, int d, int id) -> int {\n      if (!node) return 0;\n      if (d == ids.size()) ids.push_back(id);\n      return max({id - ids[d] + 1, \n                 dfs(node->left, d + 1, (id - ids[d]) * 2), \n                 dfs(node->right, d + 1, (id - ids[d]) * 2 + 1)});\n    };\n    return dfs(root, 0, 0);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  private List<Integer> ids;\n  \n  public int widthOfBinaryTree(TreeNode root) {\n    this.ids = new ArrayList<Integer>();\n    return dfs(root, 0, 0);\n  }\n  \n  private int dfs(TreeNode root, int d, int id) {\n    if (root == null) return 0;\n    if (ids.size() == d) ids.add(id);\n    return Math.max(id - ids.get(d) + 1, \n             Math.max(this.dfs(root.left, d + 1, (id - ids.get(d)) * 2),\n                      this.dfs(root.right, d + 1, (id - ids.get(d)) * 2 + 1)));\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def widthOfBinaryTree(self, root: TreeNode) -> int:\n    ids = []\n    def dfs(node: TreeNode, d: int, id: int) -> int:\n      if not node: return 0\n      if d == len(ids): ids.append(id)\n      return max(id - ids[d] + 1, \n                 dfs(node.left, d + 1, (id - ids[d]) * 2),\n                 dfs(node.right, d + 1, (id - ids[d]) * 2 + 1))\n    return dfs(root, 0, 0)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width&#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,177,28],"class_list":["post-7060","post","type-post","status-publish","format-standard","hentry","category-tree","tag-dfs","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7060","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=7060"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7060\/revisions"}],"predecessor-version":[{"id":7066,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7060\/revisions\/7066"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}