【树】四叉树交集

题目描述

给你两个四叉数据,quadTree1 和 quadTree2。 quadTree1表示一个 n*n 二进制矩阵,quadTree2表示另一个 n*n 二进制矩阵。

请返回一个表示n * n 二进制矩阵的四叉树,它是quadTree1 和 quadTree2 按位逻辑或运算的结果。

四叉树数据结构如下:

class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;
}

解题思路

先做一次演算

对这2个矩阵做计算,结果如下:

第一个矩阵通过代码表示如下:

Node quadTree1 = new Node(false, false,
        new Node(false, false,
                new Node(false, true, null, null, null, null),
                new Node(false, true, null, null, null, null),
                new Node(true, true, null, null, null, null),
                new Node(true, true, null, null, null, null)),
        new Node(true, true, null, null, null, null),
        new Node(false, true, null, null, null, null),
        new Node(true, true, null, null, null, null));

第二个矩阵通过代码表示如下:

Node quadTree2 = new Node(false, false,
        new Node(false, true, null, null, null, null),
        new Node(true, true, null, null, null, null),
        new Node(true, true, null, null, null, null),
        new Node(false, true, null, null, null, null));

结果表示如下:

Node res = new Node(false, false,
        new Node(false, false,
                new Node(false, true, null, null, null, null),
                new Node(false, true, null, null, null, null),
                new Node(true, true, null, null, null, null),
                new Node(true, true, null, null, null, null)),
        new Node(true, true, null, null, null, null),
        new Node(false, true, null, null, null, null),
        new Node(true, true, null, null, null, null));

采用递归思路,思路如下:

  1. 先判断是否都是叶子节点,如果是叶子节点,则直接计算结果并且返回
  2. 如果部分是叶子节点,则考虑:
    1. case1: 叶子节点value为true,直接拷贝叶子节点并且返回结果。
    2. case2: 叶子节点value为false,直接拷贝非叶子节点并且返回结果。
  3. 如果都不是叶子节点,则递归计算: topLeft、 topRight、 bottomLeft、 bottomRight。如果计算完成后结果都是true或者都是false,则把当前节点设置成叶子节点,设置value后,直接返回;如果结果不全是true或者全是false,则直接把计算结果返回。

代码实现

做计算的代码:


class Solution {
    public Node intersect(Node quadTree1, Node quadTree2) {


        if (quadTree1.isLeaf && quadTree2.isLeaf) {
            Node newNode = new Node();
            newNode.isLeaf = true;
            newNode.val = quadTree1.val || quadTree2.val;
            return newNode;
        } else if (quadTree1.isLeaf) {

            if (quadTree1.val) {
                return new Node(quadTree1.val, quadTree1.isLeaf, quadTree1.topLeft, quadTree1.topRight, quadTree1.bottomLeft, quadTree1.bottomRight);
            } else {
                return new Node(quadTree2.val, quadTree2.isLeaf, quadTree2.topLeft, quadTree2.topRight, quadTree2.bottomLeft, quadTree2.bottomRight);
            }
        } else if (quadTree2.isLeaf) {
            if (quadTree2.val) {
                return new Node(quadTree2.val, quadTree2.isLeaf, quadTree2.topLeft, quadTree2.topRight, quadTree2.bottomLeft, quadTree2.bottomRight);
            } else {
                return new Node(quadTree1.val, quadTree1.isLeaf, quadTree1.topLeft, quadTree1.topRight, quadTree1.bottomLeft, quadTree1.bottomRight);
            }

        } else {
            Node newNode = new Node();
            newNode.val = false;
            newNode.isLeaf = false;
            newNode.topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft);
            newNode.topRight = intersect(quadTree1.topRight, quadTree2.topRight);
            newNode.bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
            newNode.bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);

            if (newNode.topLeft.isLeaf && newNode.topRight.isLeaf && newNode.bottomLeft.isLeaf && newNode.bottomRight.isLeaf) {
                if (newNode.topLeft.val == newNode.topRight.val && newNode.topRight.val == newNode.bottomLeft.val && newNode.bottomLeft.val == newNode.bottomRight.val) {
                    return new Node(newNode.topLeft.val, true, null, null, null, null);
                }
            }
            return newNode;
        }
    }
}

四叉树的实现:


class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;

    public Node() {
    }

    public Node(boolean _val, boolean _isLeaf, Node _topLeft, Node _topRight, Node _bottomLeft, Node _bottomRight) {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = _topLeft;
        topRight = _topRight;
        bottomLeft = _bottomLeft;
        bottomRight = _bottomRight;
    }
}

总结

这道题构造用例比较麻烦,解决思路就是递归操作,这个同普通二叉树遍历是一样的。细节处理上要考虑返回结果都是true或者false时需要把当前节点变成叶子节点的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值