Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Add recursive solution for 965#126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ashmichheda wants to merge1 commit intofishercoder1534:master
base:master
Choose a base branch
Loading
fromashmichheda:965
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_965.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,4 +21,11 @@ private boolean dfs(TreeNode root, int value) {
return dfs(root.left, value) && dfs(root.right, value);
}
}
public static class Solution2 {
public boolean isUnivalTree(TreeNode root) {
boolean leftUnivaled = root.left == null || root.left.val == root.val && isUnivalTree(root.left);
boolean rightUnivaled = root.right == null || root.right.val == root.val && isUnivalTree(root.right);
return leftUnivaled && rightUnivaled;
}
}
}
18 changes: 15 additions & 3 deletionssrc/test/java/com/fishercoder/_965Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,22 +2,24 @@

import com.fishercoder.common.classes.TreeNode;
import com.fishercoder.common.utils.TreeUtils;
import com.fishercoder.solutions._14;
import com.fishercoder.solutions._965;
import java.util.Arrays;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class _965Test {
private static _965.Solution1 solution1;
private static _965.Solution2 solution2;
private static TreeNode root;

@BeforeClass
public static void setup() {

solution1 = new _965.Solution1();
solution2 = new _965.Solution2();
}

@Test
Expand All@@ -31,4 +33,14 @@ public void test2() {
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2, 5, 2));
assertEquals(false, solution1.isUnivalTree(root));
}
@Test
public void test3() {
root = TreeUtils.constructBinaryTree(Arrays.asList(-1, -1, -1, -1, -1, -1, -1));
assertEquals(true, solution2.isUnivalTree(root));
}
@Test
public void test4() {
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2, 2, 2, 2, 1));
assertEquals(false, solution2.isUnivalTree(root));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp