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

Commite356669

Browse files
count univalue subtrees
1 parent0ff1c87 commite356669

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagemedium;
2+
3+
importclasses.TreeNode;
4+
5+
publicclassCountUnivalueSubtrees {
6+
7+
publicintcountUnivalSubtrees(TreeNoderoot) {
8+
int[]count =newint[1];
9+
helper(root,count);
10+
returncount[0];
11+
}
12+
13+
privatebooleanhelper(TreeNodenode,int[]count) {
14+
if (node ==null) {
15+
returntrue;
16+
}
17+
booleanleft =helper(node.left,count);
18+
booleanright =helper(node.right,count);
19+
if (left &&right) {
20+
if (node.left !=null &&node.val !=node.left.val) {
21+
returnfalse;
22+
}
23+
if (node.right !=null &&node.val !=node.right.val) {
24+
returnfalse;
25+
}
26+
count[0]++;
27+
returntrue;
28+
}
29+
returnfalse;
30+
}
31+
32+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)|[Solution](../../blob/master/MEDIUM/src/medium/_3Sum_Smaller.java)| O(n^2)|O(1)| Medium|
6464
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../../blob/master/EASY/src/easy/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion
6565
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)|[Solution](../../blob/master/EASY/src/easy/MeetingRooms.java)| O(nlogn)| O(1)|
66+
|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)|[Solution](../../blob/master/MEDIUM/src/medium/CountUnivalueSubtrees.java)| O(n)|O(h) | Medium| DFS
6667
|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)|[Solution](../../blob/master/EASY/src/easy/GroupShiftedStrings.java)| O(nlogn)| O(n)|
6768
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[Solution](../../blob/master/EASY/src/easy/StrobogrammaticNumber.java)| O(n)| O(1)|
6869
|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[Solution](../../blob/master/EASY/src/easy/ShortestWordDistance.java)| O(n)| O(1)|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp