建立四叉树

我们想要使用一棵四叉树来储存一个 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. OpenSSL中关于RSA_new和RSA_free的内存泄漏

    这个具体的问题问题代码如下: RSA *rsaKey=RSA_new(); rsaKey = RSA_generate_key(keyBits,,NULL,NULL); RSA_free(rsaKey ...

  2. [make error ]ubuntu显示不全

    make时候,输出到文件里 make >&makelog 就会自动出现一个makelog 会慢一些,不要急.

  3. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  4. 【TensorFlow入门完全指南】模型篇·最近邻模型

    最近邻模型,更为常见的是k-最近邻模型,是一种常见的机器学习模型,原理如下: KNN算法的前提是存在一个样本的数据集,每一个样本都有自己的标签,表明自己的类型.现在有一个新的未知的数据,需要判断它的类 ...

  5. WINDOWS-基础:Thread.Sleep(0)

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...

  6. vue入坑教程(三)vue父子组件之间互相调用方法以及互相传递数据

    1.父组件调用子组件的方法 父组件: <template> <div> <button v-on:click="clickParent">点击& ...

  7. java 第11次作业:你能看懂就说明你理解了——this关键字

    this 代表当前对象

  8. sql 经典加强巩固练习题

    由于本人需要加强巩固一下数据库知识,就搜罗了一些题目来练习,感觉不错,故分享一下资源难度层度依次上升这50道里面自认为应该没有太多错误,而且尽可能使用了最简单或是最直接的查询,有多种不相上下解法的题目 ...

  9. JavaScript数组之傻傻分不清系列(split,splice,slice)

    因业务场景需求,需要将一个数组截断而不需要影响原数组.这里来理解一下 slice,splice,split slice() 从某个已有的数组返回选定的元素.(JavaScript Array 对象) ...

  10. 【上下界网络流】bzoj2502: 清理雪道

    模型:无源汇有上下界可行流 LJN:模板题吧 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的 ...