662. 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 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,null,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,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
解法一:dfs
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
vector<pair<int, int> > v;
return dfs(root, 0, 1, v);
}
private:
int dfs(TreeNode* root, int depth, int index, vector<pair<int, int> > &v) {
if(!root) return 0;
//reach the new level, start with the index
if(v.size() == depth) v.push_back(make_pair(index, index));
else v[depth].second = index;
int res = v[depth].second - v[depth].first + 1;
int resL = dfs(root->left, depth+1, index*2, v);
int resR = dfs(root->right, depth+1, index*2+1, v);
return max({res, resL, resR});
}
};
说是dfs,还不如说是前序遍历,,我一开始便想到能不能用层序遍历来解,可是苦于结构体里面没有index这一项,所以我就没有往上面想了。
后来看到有人套了一个结构体,所以我就知道了
解法二:层序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct myNode {
TreeNode* node;
int index;
myNode(TreeNode* tn, int i): node(tn), index(i) {}
};
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if(!root) return 0;
queue<myNode> q;
int globalWidth = 0;
q.push(myNode(root, 1));
while(!q.empty()) {
int size = q.size();
globalWidth = max(q.back().index - q.front().index + 1, globalWidth);
while(size--) {
myNode tmp = q.front();
q.pop();
if(tmp.node->left) q.push(myNode(tmp.node->left, tmp.index*2));
if(tmp.node->right) q.push(myNode(tmp.node->right, tmp.index*2+1));
}
}
return globalWidth;
}
};
这个解法的运用还有很多,关键点在于层序遍历的时候如何保证当前queue里面存在的只有同一层的,一种方法是获得size,然后在size里面不断pop;还有一种是再往结构体里面塞一个depth,这里就不合适了
queue<node> q;
q.push(node{root, 0});
while (!q.empty()) {
node temp = q.front();
q.pop();
//这里的result是记录每一层的vector数组
result[temp.depth].push_back(post[temp.index]);
if (tree[temp.index][0] != 0)
q.push(node{tree[temp.index][0], temp.depth + 1});
if (tree[temp.index][1] != 0)
q.push(node{tree[temp.index][1], temp.depth + 1});
}