二叉树宽度
满二叉树定义:一棵深度为k且有2^k-1个结点的二叉树称为满二叉树
所以二叉树宽度为2^k-1
怎么创建数组
int h = getHeight(root);
int w = (1<<h ) -1 ;
// 2^h -1 二叉树的最大宽度,也是字符串数组的长度
//从上面可以看出数组列数一定为奇数
怎么填写
要求将每一个节点填写到数组列数中位数上,也就是二分查找,中间位置,然后递归填写下一行。需要记录左边界left,左边界right,行数,以及节点root
代码如下
class Solution {
public List<List<String>> printTree(TreeNode root) {
int h = getHeight(root);
int w = (1<<h ) -1 ; // 2^h -1 二叉树的最大宽度,也是字符串数组的长度
List<List<String>> res = new ArrayList<>();
//先将res全部置为“”
for(int i = 0 ; i < h ; i ++) {
List<String> item = new ArrayList<>();
for(int j = 0 ; j < w ; j++)
item.add("");
res.add(item);
}
//填充数字
fill(root, res, 0, 0, w-1);
return res;
}
//返回二叉树的高度
int getHeight(TreeNode root) {
if(root == null) return 0;
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
/*
实际上相当于是一个二分,在左子树去寻找相应的位置,满足
根节点在区域中间,左孩子在左边的中间,右孩子在右边的中间
*/
void fill(TreeNode node, List<List<String>> res , int h , int l , int r) {
if(node == null) return;
//找中位数
int mid = (l +r) /2;
String s= res.get(h).get(mid);
res.get(h).set(mid, Integer.toString(node.val)); //相应位置填上数字
//递归处理
fill(node.left, res, h+1, l, mid-1);
fill(node.right, res, h+1, mid+1, r);
}
}