Maximum Width of Binary Tree问题及解法
来源:互联网 发布:kvm centos虚拟化 编辑:程序博客网 时间:2024/06/05 22:52
问题描述:
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.
示例:
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).
Input: 1 / 3 / \ 5 3 Output: 2Explanation: The maximum width existing in the third level with the length 2 (5,3).
Input: 1 / \ 3 2 / 5 Output: 2Explanation: The maximum width existing in the second level with the length 2 (3,2).
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).
问题分析:
题目主要考查节点编号问题,对于一个左子节点,其编号为父节点编号的两倍,而右子节点编号为父节点编号两倍加1.
最终遍历每层找到最左最右节点编号,取其中最大的差值加1即为答案。
过程详见代码:
/** * 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 == nullptr) return 0;int res = 0;vector<pair<int, int>> val;val.emplace_back(pair<int, int>(1, 1));dfs(root->left, val, 1, 1, 0);dfs(root->right, val, 1, 1, 1);for (auto v : val){int t = v.second - v.first + 1;if (t > res) res = t;}return res;}void dfs(TreeNode* root, vector<pair<int, int>>& val, int depth, int parent, int dirt){if (root == nullptr) return;if (val.size() <= depth) val.emplace_back(pair<int, int>(parent * 2 + dirt, parent * 2 + dirt));else val[depth].second = parent * 2 + dirt;dfs(root->left, val, depth + 1, parent * 2 + dirt, 0);dfs(root->right, val, depth + 1, parent * 2 + dirt, 1);}};
阅读全文
0 0
- Maximum Width of Binary Tree问题及解法
- Maximum Depth of Binary Tree问题及解法
- Maximum Width of Binary Tree
- Maximum Width of Binary Tree
- Maximum Binary Tree问题及解法
- Diameter of Binary Tree问题及解法
- leetcode Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- [LeetCode] 662. Maximum Width of Binary Tree
- LeetCode Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- 662. Maximum Width of Binary Tree
- Maximum Width of Binary Tree 解题心得
- 662. Maximum Width of Binary Tree
- Minimum Depth of Binary Tree问题及解法
- Average of Levels in Binary Tree问题及解法
- Verify Preorder Serialization of a Binary Tree 问题及解法
- 【LeetCode】104. Maximum Depth of Binary Tree 解法及注释,递归,深度搜索
- Android Service
- IBM Appscan9.0.3安全扫描简单安装、使用以及高危漏洞修复
- c++基础(19)
- 深入浅出Mybatis技术原理与实战: 读书笔记
- webSocket长连接实现demo(场景:扫码自动跳转登录,或者替换轮询)
- Maximum Width of Binary Tree问题及解法
- 必须知道的Android屏幕自适应解决方案
- viewwillappear和viewdidload的区别是什么
- Python(十)Python 的 Magic Method
- TreeSet、Comparable、Comparator、内部类、匿名类
- 腾讯云直播流程及腾讯云通讯功能整理
- 人工智能热浪中,直播行业也被撞了一下腰?
- wangEditor 基于javascript和css开发的 Web富文本编辑器, 轻量、简洁、易用、开源免费
- 关于List、Set、Map集合的简介(二)