Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b5d3c4

Browse filesBrowse files
committed
commit
1 parent 456c2a1 commit 4b5d3c4
Copy full SHA for 4b5d3c4

File tree

Expand file treeCollapse file tree

7 files changed

+299
-138
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+299
-138
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+2Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+144-138Lines changed: 144 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-07-06 21:40
6+
**/
7+
public class Main {
8+
@Test
9+
public void test1() {
10+
int[][] grid = {
11+
{1, 1, 1, 1, 0, 0, 0, 0},
12+
{1, 1, 1, 1, 0, 0, 0, 0},
13+
{1, 1, 1, 1, 1, 1, 1, 1},
14+
{1, 1, 1, 1, 1, 1, 1, 1},
15+
{1, 1, 1, 1, 0, 0, 0, 0},
16+
{1, 1, 1, 1, 0, 0, 0, 0},
17+
{1, 1, 1, 1, 0, 0, 0, 0},
18+
{1, 1, 1, 1, 0, 0, 0, 0},
19+
};
20+
21+
Solution solution = new Solution();
22+
Node node = solution.construct(grid);
23+
print(node);
24+
}
25+
26+
private static void print(Node node) {
27+
if (node != null) {
28+
System.out.println("(" + node.isLeaf + ", " + node.val + ")");
29+
print(node.topLeft);
30+
print(node.topRight);
31+
print(node.bottomLeft);
32+
print(node.bottomRight);
33+
}
34+
}
35+
}
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2019-07-06 21:28
4+
**/
5+
public class Node {
6+
public boolean val;
7+
public boolean isLeaf;
8+
public Node topLeft;
9+
public Node topRight;
10+
public Node bottomLeft;
11+
public Node bottomRight;
12+
13+
public Node() {
14+
}
15+
16+
public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
17+
this.val = val;
18+
this.isLeaf = isLeaf;
19+
this.topLeft = topLeft;
20+
this.topRight = topRight;
21+
this.bottomLeft = bottomLeft;
22+
this.bottomRight = bottomRight;
23+
}
24+
}
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2019-07-06 21:27
4+
**/
5+
public class Solution {
6+
public Node construct(int[][] grid) {
7+
8+
if (grid == null) {
9+
return null;
10+
}
11+
12+
Node node = new Node();
13+
14+
construct(node, grid, 0, 0, grid.length);
15+
return node;
16+
}
17+
18+
private void construct(Node node, int[][] grid, int x, int y, int size) {
19+
20+
if (size <= 0) {
21+
return;
22+
}
23+
24+
if (size == 1) {
25+
node.isLeaf = true;
26+
node.val = grid[x][y] == 1;
27+
return;
28+
}
29+
30+
int half = size / 2;
31+
32+
Node topLeft = new Node();
33+
Node topRight = new Node();
34+
Node bottomLeft = new Node();
35+
Node bottomRight = new Node();
36+
37+
38+
construct(topLeft, grid, x, y, half);
39+
construct(topRight, grid, x, y + half, half);
40+
construct(bottomLeft, grid, x + half, y, half);
41+
construct(bottomRight, grid, x+ half, y + half, half);
42+
43+
// 四个孩子都是叶子节点,并且值都相等
44+
if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf) {
45+
if (topLeft.val && topRight.val && bottomLeft.val && bottomRight.val) {
46+
node.isLeaf = true;
47+
node.val = true;
48+
return;
49+
} else if (!topLeft.val && !topRight.val && !bottomLeft.val && !bottomRight.val) {
50+
node.isLeaf = true;
51+
node.val = false;
52+
return;
53+
}
54+
}
55+
56+
node.isLeaf = false;
57+
node.topLeft = topLeft;
58+
node.topRight = topRight;
59+
node.bottomLeft = bottomLeft;
60+
node.bottomRight = bottomRight;
61+
}
62+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.