662. Maximum Width of Binary Tree
来源:互联网 发布:excel取消数据验证 编辑:程序博客网 时间:2024/06/05 18:41
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: 4Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input: 1 / 3 / \ 5 3 Output: 2Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input: 1 / \ 3 2 / 5 Output: 2Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input: 1 / \ 3 2 / \ 5 9 / \ 6 7Output: 8Explanation: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.
/** * 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) { if(root == NULL) return 0; queue<TreeNode *> real_queue; queue<int> virtual_queue; //利用实队列和虚队列 实队列保存节点信息 虚队列保存节点位置信息 LeetCode中的discuss利用递归方法 更是巧妙 int lastLayerSum = 1; int currentLayerSum = 0; int MaxSum = 0; bool first = true; // 是否为每层的第一个节点 int firstflag = 0; // 表示每层第一个节点的编号 real_queue.push(root); virtual_queue.push(1); while(!real_queue.empty()) { currentLayerSum = 0; first = true; while(lastLayerSum) { TreeNode * node = real_queue.front(); real_queue.pop(); int flag = virtual_queue.front(); virtual_queue.pop(); if(first) { firstflag = flag; first = false; } if ((flag-firstflag+1)>MaxSum) MaxSum = flag-firstflag+1; if(node->left) { real_queue.push(node->left); virtual_queue.push(2*flag); currentLayerSum ++ ; } if(node->right) { real_queue.push(node->right); virtual_queue.push(2*flag+1); currentLayerSum ++; } lastLayerSum--; } lastLayerSum = currentLayerSum; } return MaxSum; }};
int widthOfBinaryTree(TreeNode* root) { return dfs(root, 0, 1, vector<pair<int, int>>() = {}); } int dfs(TreeNode* root, int level, int order, vector<pair<int, int>>& vec){ if(root == NULL)return 0; if(vec.size() == level)vec.push_back({order, order}); else vec[level].second = order; return max({vec[level].second - vec[level].first + 1, dfs(root->left, level + 1, 2*order, vec), dfs(root->right, level + 1, 2*order + 1, vec)}); }
阅读全文
0 0
- 662. Maximum Width of Binary Tree
- [LeetCode] 662. Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- Maximum Width of Binary Tree
- Maximum Width of Binary Tree
- leetcode Maximum Width of Binary Tree
- LeetCode Maximum Width of Binary Tree
- Maximum Width of Binary Tree 解题心得
- Maximum Width of Binary Tree问题及解法
- 662. Maximum Width of Binary Tree(二叉树层次遍历,二叉树标序号)
- leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- 监听浏览器返回时刷新上一页面JavaScript JQuery
- 数据结构与算法(Java描述)-13、对象数组以及MyVector类、MySet类实现
- 图解Android和Linux发行版的区别
- 爬取新浪、网易、今日头条、UC四大网站新闻标题及内容
- java 如何读取配置文件
- 662. Maximum Width of Binary Tree
- js购物车
- mysql5.17组复制安装
- oracle中sequence详解
- Android Okhttp缓存:精细化每一个Request的CacheControl缓存控制策略(二)
- 2006年培养学员8万人,每年增长%25,请问按此增长速度,到那一年培训学员人数将达到20万人用for,while,dowhile,实现
- ansible inventory.py 脚本
- mysql数据库show tables 显示表名,但是查询的时候却提示此表不存在
- UEditor 富文本编辑器拓展之-----上传图片至项目之外或资源服务器