建立四叉树

文章详述了利用递归构建n*n矩阵的四叉树结构,区分叶节点和非叶节点,输出以层次遍历序列化形式。

给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成。请你用四叉树表示该矩阵 grid 。

你需要返回能表示矩阵 grid 的 四叉树 的根结点。

四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:

  • val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False。注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。
  • isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。
class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;
}

我们可以按以下步骤为二维区域构建四叉树:

  1. 如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
  2. 如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
  3. 使用适当的子网格递归每个子节点。

如果你想了解更多关于四叉树的内容,可以参考 wiki 。

四叉树格式:

你不需要阅读本节来解决这个问题。只有当你想了解输出格式时才会这样做。输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。

它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。

如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 

示例 1:

输入:grid = [[0,1],[1,0]]
输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
解释:此示例的解释如下:
请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。

示例 2:

输入:grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
输出:[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
解释:网格中的所有值都不相同。我们将网格划分为四个子网格。
topLeft,bottomLeft 和 bottomRight 均具有相同的值。
topRight 具有不同的值,因此我们将其再分为 4 个子网格,这样每个子网格都具有相同的值。
解释如下图所示:

提示:

  1. n == grid.length == grid[i].length
  2. n == 2x 其中 0 <= x <= 6

思路与算法

我们可以使用递归的方法构建出四叉树。

具体地,我们用递归函数\text{dfs}(r_0, c_0, r_1, c_1)处理给定的矩阵\textit{grid}r_0行开始到r_1-1行,从c_0c_1-1列的部分。我们首先判定这一部分是否均为01,如果是,那么这一部分对应的是一个叶节点,我们构造出对应的叶节点并结束递归;如果不是,那么这一部分对应的是一个非叶节点,我们需要将其分成四个部分:行的分界线为\dfrac{r_0+r_1}{2} ,列的分界线为\dfrac{c_0+c_1}{2},根据这两条分界线递归地调用 dfs\text{dfs}dfs 函数得到四个部分对应的树,再将它们对应地挂在非叶节点的四个子节点上。

代码

class Solution {
public:
    Node *construct(vector<vector<int>> &grid) {
        function<Node*(int, int, int, int)> dfs = [&](int r0, int c0, int r1, int c1) {
            for (int i = r0; i < r1; ++i) {
                for (int j = c0; j < c1; ++j) {
                    if (grid[i][j] != grid[r0][c0]) { // 不是叶节点
                        return new Node(
                                true,
                                false,
                                dfs(r0, c0, (r0 + r1) / 2, (c0 + c1) / 2),
                                dfs(r0, (c0 + c1) / 2, (r0 + r1) / 2, c1),
                                dfs((r0 + r1) / 2, c0, r1, (c0 + c1) / 2),
                                dfs((r0 + r1) / 2, (c0 + c1) / 2, r1, c1)
                        );
                    }
                }
            }
            // 是叶节点
            return new Node(grid[r0][c0], true);
        };
        return dfs(0, 0, grid.size(), grid.size());
    }
};


复杂度分析

时间复杂度:O(n^2 \log n)。这里给出一个较为宽松的时间复杂度上界。记T(n)为边长为n的数组需要的时间复杂度,那么「判定这一部分是否均为01」需要的时间为O(n^2),在这之后会递归调用 4规模为n/2的子问题,那么有:

T(n) = 4T(n/2) + O(n^2)
以及:

T(1) = O(1)
根据主定理,可以得到T(n) = O(n^2 \log n)。但如果判定需要的时间达到了渐近紧界\Theta(n^2),那么说明这一部分包含的元素大部分都是相同的,也就是说,有很大概率在深入递归时遇到元素完全相同的一部分,从而提前结束递归。因此O(n^2 \log n)的时间复杂度是很宽松的,实际运行过程中可以跑出与方法二O(n^2)时间复杂度代码相似的速度。

空间复杂度:O(\log n),即为递归需要使用的栈空间。

在力扣中,“建立四叉树”问题(LeetCode 427)是将一个 `n x n` 的二进制矩阵 `grid` 构建成一个四叉树四叉树是一种树数据结构,其中每个内部节点恰好有四个子节点:`topLeft`、`topRight`、`bottomLeft` 和 `bottomRight`。每个节点还有一个布尔属性 `isLeaf`,表示该节点是否为叶子节点。 以下是该问题的 C++ 解答: ```cpp #include <vector> // 定义四叉树节点结构 class Node { public: bool val; bool isLeaf; Node* topLeft; Node* topRight; Node* bottomLeft; Node* bottomRight; Node() { val = false; isLeaf = false; topLeft = nullptr; topRight = nullptr; bottomLeft = nullptr; bottomRight = nullptr; } Node(bool _val, bool _isLeaf) { val = _val; isLeaf = _isLeaf; topLeft = nullptr; topRight = nullptr; bottomLeft = nullptr; bottomRight = nullptr; } Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft = _topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight = _bottomRight; } }; class Solution { public: Node* construct(std::vector<std::vector<int>>& grid) { return buildTree(grid, 0, 0, grid.size()); } Node* buildTree(const std::vector<std::vector<int>>& grid, int x, int y, int len) { if (len == 1) { return new Node(grid[x][y] == 1, true); } int halfLen = len / 2; Node* topLeft = buildTree(grid, x, y, halfLen); Node* topRight = buildTree(grid, x, y + halfLen, halfLen); Node* bottomLeft = buildTree(grid, x + halfLen, y, halfLen); Node* bottomRight = buildTree(grid, x + halfLen, y + halfLen, halfLen); if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf && topLeft->val == topRight->val && topRight->val == bottomLeft->val && bottomLeft->val == bottomRight->val) { bool val = topLeft->val; delete topLeft; delete topRight; delete bottomLeft; delete bottomRight; return new Node(val, true); } return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight); } }; ``` ### 代码解释 1. **Node 类**:定义了四叉树的节点结构,包含节点的值 `val`、是否为叶子节点 `isLeaf` 以及四个子节点指针。 2. **construct 函数**:作为入口函数,调用 `buildTree` 函数开始构建四叉树。 3. **buildTree 函数**:递归地构建四叉树。如果当前区域的大小为 1,则创建一个叶子节点。否则,递归地构建四个子区域的四叉树。如果四个子节点都是叶子节点且值相同,则合并为一个叶子节点;否则,创建一个非叶子节点。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wanderer001

ROIAlign原理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值