建立四叉树

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

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

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

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

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

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

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

提示:

  1. N 将小于 1000 且确保是 2 的整次幂。
  2. 如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。
 /*
// 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() {} 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;
}
};
*/
class Solution {
public Node construct(int[][] grid) {
return build(grid,0,0,grid.length);
} public Node build(int[][] grid,int x,int y,int len){
if(len<=0) return null;
for(int i=x;i<x+len;++i){
for(int j=y;j<y+len;++j){
if(grid[i][j]!=grid[x][y]){
return new Node(true,false,build(grid,x,y,len/2),
build(grid,x,y+len/2,len/2),
build(grid,x+len/2,y,len/2),
build(grid,x+len/2,y+len/2,len/2));
}
}
}
return new Node(grid[x][y]==1,true,null,null,null,null);
}
}

Leetcode 427.建立四叉树的更多相关文章

  1. Java实现 LeetCode 427 建立四叉树

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

  2. [LeetCode] Construct Quad Tree 建立四叉树

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  3. LeetCode 427 Construct Quad Tree 解题报告

    题目要求 We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be tru ...

  4. leetcode 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  5. 【LeetCode】未分类(tag里面没有)(共题)

    [334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...

  6. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. 基础数据结构之(Binary Trees)

    从头开始刷ACM,真的发现过去的很多漏洞,特别越是基础的数据结构,越应该学习得精,无论是ACM竞赛,研究生考试,还是工程上,对这些基础数据结构的应用都非常多,深刻理解非常必要.不得不说最近感触还是比较 ...

  9. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. Android篇---Styles和Themes常见用法可能疑点小结

    1.style和theme的区别: 简而言之,style指的就是安卓中一个UI控件的样式,而themes指的是安卓中一个activity界面或者整个安卓应用整体的样式.theme的范围比style的范 ...

  2. [tensorflow] tf2.0 简单例子

    tf2.0笔记 感觉,都统一了,pytorch tensorflow mxnet,大家都差不多了 gan例子笔记 import tensorflow as tf from tensorflow.ker ...

  3. [论文理解]SSD:Single Shot MultiBox Detector

    SSD:Single Shot MultiBox Detector Intro SSD是一套one-stage算法实现目标检测的框架,速度很快,在当时速度超过了yolo,精度也可以达到two-stag ...

  4. 2018.3.5 Java语言基础与面向对象编程实践

    Java语言基础与面向对象编程实践 第一章 初识Java 1.Java特点 http://www.manew.com/blog-166576-20164.html Java语言面向对象的 Java语言 ...

  5. JS实用技术

    JS外部引用其他文件(建议) <script src="myScript1.js"></script> JS输出显示方式 使用 window.alert() ...

  6. java基础—GUI编程(一)

    一.AWT介绍

  7. JavaScript的基础知识

    1,标识符 标识符是程序中常量或变量命名的一种术语称呼,并不是所有的字符组成都是一个合法的标识符,规范如下: 标识符的组成部分可以是字母,数字,下划线或美元($)符号 标识符开头是字母,下划线或美元( ...

  8. 【转】Matlab的regionprops详解

    matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...

  9. CE软件修改器

    下载地址: 链接:https://pan.baidu.com/s/1WQa5epfmLW92xk0XY10pqw 提取码:jt3k 喜欢请点赞

  10. 初学redis,redis基本数据类型

    String: 1. set key value 2. get key 3. del key 4. strlen key 5. getset key value 修改键值对   6. getrange ...