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

Commita0f55be

Browse files
largest BST subtree
1 parent812cf1e commita0f55be

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagemedium;
2+
3+
importclasses.TreeNode;
4+
5+
publicclassLargestBSTSubtree {
6+
7+
publicintlargestBSTSubtree(TreeNoderoot) {
8+
if(root ==null)return0;
9+
if(isBST(root))returngetNodes(root);
10+
returnMath.max(find(root.left),find(root.right));
11+
}
12+
13+
intfind(TreeNoderoot){
14+
if(isBST(root))returngetNodes(root);
15+
returnMath.max(find(root.left),find(root.right));
16+
}
17+
18+
intgetNodes(TreeNoderoot){
19+
if(root ==null)return0;
20+
returndfsCount(root);
21+
}
22+
23+
intdfsCount(TreeNoderoot){
24+
if(root ==null)return0;
25+
returndfsCount(root.left) +dfsCount(root.right) +1;
26+
}
27+
28+
booleanisBST(TreeNoderoot){
29+
if(root ==null)returntrue;
30+
returndfs(root,Long.MIN_VALUE,Long.MAX_VALUE);
31+
}
32+
33+
booleandfs(TreeNoderoot,longmin,longmax){
34+
if(root ==null)returntrue;
35+
if(root.val <=min ||root.val >=max)returnfalse;
36+
returndfs(root.left,min,root.val) &&dfs(root.right,root.val,max);
37+
}
38+
39+
}

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
4040
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../../blob/master/EASY/src/easy/MovingAveragefromDataStream.java)| O(1)|O(w)) | Easy| Queue
4141
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../../blob/master/EASY/src/easy/NestedListWeightSum.java)| O(n)|O(h)) | Easy| DFS
42-
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../../blob/master/MEDIUM/src/medium/CountingBits.java)| O(n)|O(n)| Medium|
42+
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../../blob/master/MEDIUM/src/medium/CountingBits.java)| O(nlogn)|O(h)| Medium|
43+
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)|[Solution](../../blob/master/MEDIUM/src/medium/LargestBSTSubtree.java)| O(n)|O(n)| Medium|
4344
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
4445
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../blob/master/MEDIUM/src/medium/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
4546
|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[Solution](../../blob/master/MEDIUM/src/medium/SparseMatrixMultiplication.java)| O(m*n*l)|O(m*l)| Medium|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp