Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Given the root of a binary tree, return the maximum width of the given tree.

The maximum width of a tree is the maximum width among all levels.

The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes are also counted into the length calculation.

It is guaranteed that the answer will in the range of 32-bit signed integer.

 

Example 1:

Input: root = [1,3,2,5,3,null,9]
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

Input: root = [1,3,null,5,3]
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:

Input: root = [1,3,2,5]
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).

 

Constraints:

  • The number of nodes in the tree is in the range [1, 3000].
  • -100 <= Node.val <= 100

Companies:
Amazon, Google, Facebook, Adobe

Related Topics:
Tree, Depth-First Search, Breadth-First Search, Binary Tree

Solution 1. Pre-order Traversal

// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(H)
class Solution {
    typedef unsigned long long ULL;
    vector<vector<ULL>> v;
    ULL ans = 0;
    void dfs(TreeNode *node, int lv, ULL nodeId) {
        if (!node) return;
        if (v.size() <= lv) v.push_back({ULLONG_MAX, 0});
        v[lv][0] = min(v[lv][0], nodeId);
        v[lv][1] = max(v[lv][1], nodeId);
        ans = max(ans, v[lv][1] - v[lv][0] + 1);
        dfs(node->left, lv + 1, 2 * nodeId);
        dfs(node->right, lv + 1, 2 * nodeId + 1);
    }
public:
    int widthOfBinaryTree(TreeNode* root) {
        if (!root) return 0;
        dfs(root, 0, 0);
        return ans;
    }
};

Solution 2. Level-order Traversal

// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
    typedef unsigned long long ULL;
public:
    int widthOfBinaryTree(TreeNode* root) {
        if (!root) return 0;
        queue<pair<TreeNode*, ULL>> q{{{root, 0}}};
        ULL ans = 0;
        while (q.size()) {
            ULL cnt = q.size(), minId, maxId; 
            for (int i = 0; i < cnt; ++i) {
                auto [node, nodeId] = q.front();
                q.pop();
                if (node->left) q.emplace(node->left, nodeId * 2);
                if (node->right) q.emplace(node->right, nodeId * 2 + 1);
                if (i == 0) minId = nodeId;
                if (i == cnt - 1) maxId = nodeId;
            }
            ans = max(ans, maxId - minId + 1);
        }
        return ans;
    }
};

Solution 3. Level-order Traversal

// OJ: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        if (!root) return 0;
        queue<pair<TreeNode*, long>> q{{{root, 0}}};
        long ans = 0;
        while (q.size()) {
            long cnt = q.size(), offset = -1, last;
            while (cnt--) {
                auto [n, i] = q.front();
                if (offset == -1) offset = i;
                i -= offset;
                last = i;
                q.pop();
                if (n->left) q.emplace(n->left, 2 * i);
                if (n->right) q.emplace(n->right, 2 * i + 1);
            }
            ans = max(ans, last + 1);
        }
        return ans;
    }
};