本文主要是介绍leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/
题解
自己写的没通过,这题不方便构造测试用例,没有找到问题在哪儿。后来看了答案:https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/discuss/157532/Java-concise-code-beat-100
class Solution {public Node intersect(Node n1, Node n2) {if (n1.isLeaf) return n1.val ? n1 : n2;if (n2.isLeaf) return n2.val ? n2 : n1;n1.topLeft = intersect(n1.topLeft, n2.topLeft);n1.topRight = intersect(n1.topRight, n2.topRight);n1.bottomLeft = intersect(n1.bottomLeft, n2.bottomLeft);n1.bottomRight = intersect(n1.bottomRight, n2.bottomRight);// try merge leafif (n1.topLeft.isLeaf && n1.topRight.isLeaf && n1.bottomLeft.isLeaf && n1.bottomRight.isLeaf &&n1.topLeft.val == n1.topRight.val && n1.topRight.val == n1.bottomLeft.val && n1.bottomLeft.val == n1.bottomRight.val) {n1.isLeaf = true;n1.val = n1.topRight.val;n1.topLeft = null;n1.topRight = null;n1.bottomLeft = null;n1.bottomRight = null;}return n1;}
}
这篇关于leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!