LintCode 1101: Maximum Width of Binary Tree

  1. Maximum Width of Binary Tree

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 full binary tree, but some nodes are null.

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 null nodes between the end-nodes are also counted into the length calculation.

Example
Example 1:

Input:

       1
     /   \
    3     2
   / \     \  
  5   3     9 

Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,#,9).
Example 2:

Input:

      1
     /  
    3    
   / \       
  5   3     

Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:

Input:

      1
     / \
    3   2 
   /        
  5      

Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:

Input:

      1
     / \
    3   2
   /     \  
  5       9 
 /         \
6           7

Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,#,#,#,#,#,#,7).
Notice
The answer will be in the range of 32-bit signed integer.

解法1:
把空节点什么都全部打入到queue中,然后用一个vector统计

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root
     * @return: the maximum width of the given tree
     */
    int widthOfBinaryTree(TreeNode * root) {
        if (!root) return 0;
        int gMaxWidth = 0;
        queue<TreeNode *> q;
        q.push(root);
        
        while(!q.empty()) {
            int qSize = q.size();
            
            vector<int> validArray;
            for (int i = 0; i < qSize; ++i) {
                TreeNode * node = q.front();
                q.pop();
                if (!node) {
                    validArray.push_back(0);
                    q.push(NULL);
                    q.push(NULL);
                    continue;
                } else {
                    validArray.push_back(1);
                }
                
                if (node->left) q.push(node->left);
                else q.push(NULL);
                if (node->right) q.push(node->right);
                else q.push(NULL);
            }
            int maxWidth = validArray.size();
            for (int i = validArray.size() - 1; i >= 0; --i) {
                if (validArray[i] == 1) break;
                else maxWidth--;
            }
            for (int i = 0; i < validArray.size(); ++i) {
                if (validArray[i] == 1) break;
                else maxWidth--;
            }
            gMaxWidth = max(gMaxWidth, maxWidth);
        }
        
        return gMaxWidth;
    }
};

解法2:参考网上。
注意节点的值没有什么用,可以用来存序号。根节点序号为i,则其左节点序号为2i,右节点序号为2i+1。每次把queue中的q.back()-q.front()+1算出来与gMaxWidth比较即可。
代码同步在
https://github.com/luqian2017/Algorithm

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root
     * @return: the maximum width of the given tree
     */
    int widthOfBinaryTree(TreeNode * root) {
        if (!root) return 0;
        int gMaxWidth = 0;
        queue<TreeNode *> q;
        root->val = 1; 
        q.push(root);
        
        while(!q.empty()) {
            int qSize = q.size();
            gMaxWidth = max(gMaxWidth, q.back()->val - q.front()->val + 1);    
            for (int i = 0; i < qSize; ++i) {
                TreeNode * node = q.front();
                q.pop();
        
                if (node->left) {
                    node->left->val = node->val * 2;
                    q.push(node->left);
                }
                if (node->right) {
                    node->right->val = node->val * 2 + 1;
                    q.push(node->right);
                }                
            }
        }
        
        return gMaxWidth;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值