leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
来源:互联网 发布:拍大师登录网络超时 编辑:程序博客网 时间:2024/06/05 23:49
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).
那么其实只要我们知道了每一层中最左边和最右边的结点的位置,我们就可以算出这一层的宽度了。所以这道题的关键就是要记录每一层中最左边结点的位置,我们知道对于一棵完美二叉树,如果根结点是深度1,那么每一层的结点数就是2*n-1,那么每个结点的位置就是[1, 2*n-1]中的一个,假设某个结点的位置是i,那么其左右子结点的位置可以直接算出来,为2*i和2*i+1,可以自行带例子检验。由于之前说过,我们需要保存每一层的最左结点的位置,那么我们使用一个数组start,由于数组是从0开始的,我们就姑且认定根结点的深度为0,不影响结果。我们从根结点进入,深度为0,位置为1,进入递归函数。
首先判断,如果当前结点为空,那么直接返回,然后判断如果当前深度大于start数组的长度,说明当前到了新的一层的最左结点,我们将当前位置存入start数组中。然后我们用idx - start[h] + 1来更新结果res。这里idx是当前结点的位置,start[h]是当前层最左结点的位置。然后对左右子结点分别调用递归函数,注意左右子结点的位置可以直接计算出来,参见代码如下:
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: int widthOfBinaryTree(TreeNode* root) { int maxWidth = 0; vector<int> start; dfs(root, 0, 1, start, maxWidth); return maxWidth; } void dfs(TreeNode* root, int depth, int index, vector<int>& start, int& maxWidth) { if (root == NULL) return; else { if (depth >= start.size()) start.push_back(index); maxWidth = max(maxWidth, index - start[depth] + 1); dfs(root->left, depth + 1, 2 * index, start, maxWidth); dfs(root->right, depth + 1, 2 * index + 1, start, maxWidth); } }};
- leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
- [LeetCode] 662. Maximum Width of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree DFS深度优先搜索
- leetcode 111. Minimum Depth of Binary Tree DFS深度优先遍历 + 添加对叶子节点判断
- leetcode 543. Diameter of Binary Tree 最长树的片段 + 深度优先遍历DFS
- leetcode Maximum Width of Binary Tree
- LeetCode Maximum Width of Binary Tree
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- leetcode 655. Print Binary Tree 深度优先遍历DFS
- leetcode 572. Subtree of Another Tree 深度优先遍历DFS
- 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 124. Binary Tree Maximum Path Sum 最大路径和 + DFS深度优先搜索
- leetcode 606. Construct String from Binary Tree 前序遍历 + 深度优先遍历DFS
- 文件权限
- unity编辑器扩展篇-将图片数字和英文转换成字体文件
- Android 使用SwitchCompat 遇到奇怪的崩溃问题
- 虚拟机上设置CentOS 7的问题记录
- Mac下使用Keras plot_model函数时出错的解决办法
- leetcode 662. Maximum Width of Binary Tree 深度优先遍历DFS
- neutron使用的数据库(sqlalchemy)操作文档
- 37两个链表的第一个公共节点
- ToastUtils统一管理类
- -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
- 数组重新分组将京东E卡排列在前面
- celery 配置手册
- NGUI文本列表TextList
- 展示系统应用和用户应用的列表