{"id":22513680,"url":"https://github.com/emahtab/maximum-width-of-binary-tree","last_synced_at":"2026-02-07T14:31:36.234Z","repository":{"id":79525651,"uuid":"237801996","full_name":"eMahtab/maximum-width-of-binary-tree","owner":"eMahtab","description":"Calculate the maximum width of Binary Tree","archived":false,"fork":false,"pushed_at":"2020-06-12T05:58:21.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-23T13:31:05.058Z","etag":null,"topics":["binary-tree","dfs","leetcode","problem-solving","width-of-binary-tree"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-02T16:42:34.000Z","updated_at":"2020-06-12T05:58:23.000Z","dependencies_parsed_at":"2023-05-10T17:15:29.011Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/maximum-width-of-binary-tree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/maximum-width-of-binary-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmaximum-width-of-binary-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmaximum-width-of-binary-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmaximum-width-of-binary-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmaximum-width-of-binary-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/maximum-width-of-binary-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmaximum-width-of-binary-tree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29197022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T12:38:28.597Z","status":"ssl_error","status_checked_at":"2026-02-07T12:38:23.888Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-tree","dfs","leetcode","problem-solving","width-of-binary-tree"],"created_at":"2024-12-07T03:13:58.671Z","updated_at":"2026-02-07T14:31:36.215Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Maximum Width of Binary Tree\n## https://leetcode.com/problems/maximum-width-of-binary-tree\n\nGiven 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 full binary tree, but some nodes are null.\n\nThe 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 null nodes between the end-nodes are also counted into the length calculation.\n```\nExample 1:\n\nInput: \n\n           1\n         /   \\\n        3     2\n       / \\     \\  \n      5   3     9 \n\nOutput: 4\nExplanation: The maximum width existing in the third level with the length 4 (5,3,null,9).\n\nExample 2:\n\nInput: \n\n          1\n         /  \n        3    \n       / \\       \n      5   3     \n\nOutput: 2\nExplanation: The maximum width existing in the third level with the length 2 (5,3).\n\nExample 3:\n\nInput: \n\n          1\n         / \\\n        3   2 \n       /        \n      5      \n\nOutput: 2\nExplanation: The maximum width existing in the second level with the length 2 (3,2).\n\nExample 4:\n\nInput: \n\n          1\n         / \\\n        3   2\n       /     \\  \n      5       9 \n     /         \\\n    6           7\nOutput: 8\nExplanation:\nThe maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).\n```\n\n**Note: Answer will be in the range of 32-bit signed integer.**\n\n## Implementation 1 : Iterative , Time : O(nodes in tree) , Space : O(nodes in tree)\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    class Node {\n        TreeNode treeNode;\n        int position;\n        Node(TreeNode treeNode, int position) {\n            this.treeNode = treeNode; this.position = position;\n        }\n    }\n    public int widthOfBinaryTree(TreeNode root) {\n        if(root == null)\n            return 0;\n        Queue\u003cNode\u003e q = new LinkedList\u003c\u003e();\n        int maxWidth = 1;\n        q.add(new Node(root, 0));\n        while(!q.isEmpty()) {\n            int size = q.size();\n            int left = 0, right = 0;\n            for(int i = 0; i \u003c size; i++) {\n                Node node = q.remove();\n                if(i == 0)\n                    left = node.position;\n                if(i == size-1)\n                    right = node.position;\n                if(node.treeNode.left != null)\n                    q.add(new Node(node.treeNode.left, 2 * node.position));\n                if(node.treeNode.right != null)\n                    q.add(new Node(node.treeNode.right, 2 * node.position + 1));\n            }\n            int width = (right - left) + 1;\n            maxWidth = Math.max(maxWidth, width);\n        }\n       return maxWidth; \n    }\n}\n```\n\n## Implementation 2 : Recursive , Time : O(nodes in tree) , Space : O(nodes in tree)\n\n```java\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n    int maxWidth = 0;\n    Map\u003cInteger, Integer\u003e leftmostNodePosition = new HashMap\u003c\u003e();\n    \n    public int widthOfBinaryTree(TreeNode root) {\n        if(root == null)\n            return 0;\n        dfs(root, 0, 0);\n        return maxWidth;\n    }\n    public void dfs(TreeNode root, int depth, int position) {\n        if (root == null) return;\n        leftmostNodePosition.putIfAbsent(depth, position);\n        maxWidth = Math.max(maxWidth, position - leftmostNodePosition.get(depth) + 1);\n        dfs(root.left, depth + 1, 2 * position);\n        dfs(root.right, depth + 1, 2 * position + 1);\n    }\n}\n\n```\n\n# References :\nhttps://www.youtube.com/watch?v=sm4UdCO2868\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmaximum-width-of-binary-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fmaximum-width-of-binary-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmaximum-width-of-binary-tree/lists"}