1. 题目

原题链接

给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节
点为空。
每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。
示例 1:

输入:
1
/
3 2
/ \ \
5 3 9
输出: 4
解释: 最大值出现在树的第 3 层,宽度为 4 (5,3,null,9)。

示例 2:

输入:
1
/
3
/ \
5 3
输出: 2
解释: 最大值出现在树的第 3 层,宽度为 2 (5,3)。

2. 题解

2.1 解法1: DFS

设置全局变量 ans, map用于保存每层的最左节点的位置值, 最早遍历到的即为最左, 这里可以使用 map 的 putIfAbsent 函数,
该函数若 key 不存在, 则放入, 若存在, 则不进行任何操作

递归函数

参数: 当前遍历结点 root, 当前深度 depth, 当前结点索引位置 index
作用: 计算最大宽度
调用过程: 更新 map, 滚动计算最大宽度

    class Solution {
        Map<Integer, Integer> map = new HashMap<>();
        int ans = 0;

        public int widthOfBinaryTree(TreeNode root) {
            dfs(root, 1, 0);
            return ans;
        }

        public void dfs(TreeNode root, int index, int depth) {
            if (root == null) return;
            if (!map.containsKey(depth)) {
                map.put(depth, index);
            }
            ans = Math.max(ans, index - map.get(depth) + 1);
            dfs(root.left, index * 2, depth + 1);
            dfs(root.right, index * 2 + 1, depth + 1);
        }
    }

参考: 官方题解

2.2 解法2: 层序遍历

主要思路: 由于形状与满二叉树相同, 遍历过程中, 给每个节点进行编号并记录, 若当前结点为 i, 则左儿子为 2i, 右儿子为 2i+1, 每次遍历一层, 遍历3完一层后, 记录的最后一个值和最前一个值, 分别为该层的最右节点和最左节点, 两者相减即为该层宽度, 然后滚动比较即可得到最大宽度

注意点: ans需要初始化为1

写法1:

class Solution {
    public int widthOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int ans = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        LinkedList<Integer> indexQueue = new LinkedList<>();
        queue.offer(root);
        indexQueue.offer(1);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode temp = queue.poll();
                int index = indexQueue.poll();
                if (temp.left != null) {
                    queue.offer(temp.left);
                    indexQueue.offer(index * 2);
                }
                if (temp.right != null) {
                    queue.offer(temp.right);
                    indexQueue.offer(index * 2 + 1);
                }
            }
            if (indexQueue.size() > 1) {
                ans = Math.max(ans, indexQueue.peekLast() - indexQueue.peekFirst() + 1);
            }
        }
        return ans;
    }
}

写法2:

    class Solution {
        public int widthOfBinaryTree(TreeNode root) {
            if (root == null) {
                return 0;
            }
            Queue<TreeNode> treeQue = new LinkedList<>();
            LinkedList<Integer> indexQue = new LinkedList<>();
            treeQue.offer(root);
            indexQue.addLast(1);
            int ans = 1;
            while (!treeQue.isEmpty()) {
                int size = treeQue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode temp = treeQue.poll();
                    int index = indexQue.removeFirst();
                    if (temp.left != null) {
                        treeQue.offer(temp.left);
                        indexQue.addLast(2 * index);
                    }
                    if (temp.right != null) {
                        treeQue.offer(temp.right);
                        indexQue.addLast(2 * index + 1);
                    }
                }
                // 每遍历完一层, 计算该层宽度, 同时当该层节点数大于 1 时才计算
                if (indexQue.size() > 1) {
                    ans = Math.max(ans, indexQue.peekLast() - indexQue.peekFirst() + 1);
                }
            }
            return ans;
        }
    }

参考: 层次遍历实现

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐