427. 建立四叉树

我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的.

每个结点还有另外两个布尔变量: isLeaf 和 val。isLeaf 当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。

你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题:

给定下面这个8 x 8 网络,我们将这样建立一个对应的四叉树:

由上文的定义,它能被这样分割:

对应的四叉树应该像下面这样,每个结点由一对 (isLeaf, val) 所代表.

对于非叶子结点,val 可以是任意的,所以使用 * 代替。

提示:

N 将小于 1000 且确保是 2 的整次幂。

其实这个题有一些像前段时间看到得棋盘覆盖

/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight; public Node() {
this.val = false;
this.isLeaf = false;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
} public Node(boolean val, boolean isLeaf) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
} public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
};
*/
class Solution {
public Node construct(int[][] grid) {
return construct(grid, 0, grid.length - 1, 0, grid.length - 1);
} private Node construct(int[][] grid, int top, int bottom, int left, int right) {
for (int i = top; i <= bottom; i++) {
for (int j = left; j <= right; j++) {
if (grid[i][j] != grid[top][left]) {
Node node = new Node(false, false);
int mid1 = top + ((bottom - top) >> 1), mid2 = left + ((right - left) >> 1);
node.topLeft = construct(grid, top, mid1, left, mid2);
node.topRight = construct(grid, top, mid1, mid2 + 1, right);
node.bottomLeft = construct(grid, mid1 + 1, bottom, left, mid2);
node.bottomRight = construct(grid, mid1 + 1, bottom, mid2 + 1, right);
return node;
}
}
}
return new Node(grid[top][left] == 1, true);
} }

Java实现 LeetCode 427 建立四叉树的更多相关文章

  1. Leetcode 427.建立四叉树

    建立四叉树 我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络.网络中每一格的值只会是真或假.树的根结点代表整个网络.对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的. 每 ...

  2. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. React Native 架构一览

    一.架构设计 整体上分为三大块,Native.JavaScript 与 Bridge: Native 管理 UI 更新及交互,JavaScript 调用 Native 能力实现业务功能,Bridge ...

  2. 一分钟掌握MySQL的InnoDB引擎B+树索引

    MySQL的InnoDB索引结构采用B+树,B+树什么概念呢,二叉树大家都知道,我们都清楚随着叶子结点的不断增加,二叉树的高度不断增加,查找某一个节点耗时就会增加,性能就会不断降低,B+树就是解决这个 ...

  3. 题解 洛谷P1562 【还是N皇后】

    原题:洛谷P1562 这个题的原理和8皇后的原理是一模一样的,就是必须要用n个皇后把每一个行填满,同时满足每一列,每一行,每一条对角线只有一个棋子.但如果按照原来的方法暴打的话只有60分(优化亲测无效 ...

  4. 对background: url("~assets/img/common/collect.svg") 0 0/14px 14px 的理解

    需求:给收藏数字前面通过::before伪元素添加图标 相关代码: .goods-info .collect { position: relative; } .goods-info .collect: ...

  5. Win10最常用的快捷键,效率Max提高100%(常用的应该是最全的)

    写在最前面 这是博主爆肝了一晚上给写出来,因为很多博客和资料中仍然使用的xp win7 和win8 的快捷键,我不断地的实验和尝试,总结出以下的快捷键,希望可以帮助到你. 最后,未经运营,爆肝博文不得 ...

  6. Dotnet core使用JWT认证授权最佳实践(二)

    最近,团队的小伙伴们在做项目时,需要用到JWT认证.遂根据自己的经验,整理成了这篇文章,用来帮助理清JWT认证的原理和代码编写操作. 第一部分:Dotnet core使用JWT认证授权最佳实践(一) ...

  7. Django之form.Form字段校验

    RegexValidator校验器: 在自定义的form组件类设置字段validators的值,引入RegexValidator模块 from django import forms from dja ...

  8. Django之JSON数据格式

    JSON简介: o    JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) o    JSON 是轻量级的文本数据交换格式 o    JSON ...

  9. linux -- 一般使用经验(四)

    一.使用grep进行条件筛选(主要为日志) 1.grep  -E '条件1|条件2|条件3'   文件名 (cat -n 文件.log |grep -E '2020-01-16 15:24:48|条件 ...

  10. 1.scrapy框架

    Scrapy 是一个基于 Twisted 的异步处理框架.异步就是说调用在发出之后,这个调用就直接返回,不管有没有结果.(非阻塞关注的是程序在等待调用结果(消息.返回值)时的状态,指在不能立刻得到结果 ...