建立四叉树

我们想要使用一棵四叉树来储存一个 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. 谷歌浏览器Chrome developer tool详细介绍

    http://www.cr173.com/html/19114_4.html 第 4 页 js调试源码控制面板 5 源码控制面板(js调试) Javascript的调试,基本上是通过源码控制面板和命令 ...

  3. POJ 2184 Cow Exhibition 奶牛展(01背包,变形)

    题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上, ...

  4. 使用代码获得Netweaver里某个software component和C4C的版本

    有同事问如何通过代码的方式获得Netweaver里某个Software component的版本信息,以及Cloud for Customer(C4C)的版本信息. Netweaver 点了Detai ...

  5. 《实战Python网络爬虫》- 感想

    端午节假期过了,之前一直在做出行准备,后面旅游完又休息了一下,最近才恢复状态. 端午假期最后一天收到一个快递,回去打开,发现是微信抽奖中的一本书,黄永祥的<实战Python网络爬虫>. 去 ...

  6. IEDA的安装与破解

    百度搜索IDEA 进入官网下载 https://www.jetbrains.com/idea/download/#section=windows 2.下载好双击即可 3. 完成破解

  7. java基础—配置环境变量

    前言 学习java的第一步就要搭建java的学习环境,首先是要安装JDK,JDK安装好之后,还需要在电脑上配置"JAVA_HOME”."path”."classpath& ...

  8. JavaScript无提示关闭当前页面窗口,兼容IE/Firefox/Chrome

    <script type="text/javascript" language="javascript"> function fc(){ var b ...

  9. UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息

    使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...

  10. IDEA项目显示树形结构