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

Commit b8e4a00

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 19.5 MB (47.25%)
1 parent cd41148 commit b8e4a00

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

‎0333-largest-bst-subtree/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>Given the root of a binary tree, find the largest <span data-keyword="subtree">subtree</span>, which is also a Binary Search Tree (BST), where the largest means subtree has the largest number of nodes.</p>
2+
3+
<p>A <strong>Binary Search Tree (BST)</strong> is a tree in which all the nodes follow the below-mentioned properties:</p>
4+
5+
<ul>
6+
<li>The left subtree values are less than the value of their parent (root) node&#39;s value.</li>
7+
<li>The right subtree values are greater than the value of their parent (root) node&#39;s value.</li>
8+
</ul>
9+
10+
<p><strong>Note:</strong> A subtree must include all of its descendants.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/10/17/tmp.jpg" style="width: 571px; height: 302px;" /></strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> root = [10,5,15,1,8,null,7]
19+
<strong>Output:</strong> 3
20+
<strong>Explanation: </strong>The Largest BST Subtree in this case is the highlighted one. The return value is the subtree&#39;s size, which is 3.</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> root = [4,2,7,2,3,5,null,2,null,null,null,null,null,1]
26+
<strong>Output:</strong> 2
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
34+
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
35+
</ul>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Follow up:</strong> Can you figure out ways to solve it with <code>O(n)</code> time complexity?</p>

‎0333-largest-bst-subtree/solution.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def is_valid_bst(self, root: Optional[TreeNode]) -> bool:
3+
"""Check if given tree is a valid BST using in-order traversal."""
4+
# An empty tree is a valid Binary Search Tree.
5+
if not root:
6+
return True
7+
8+
# If left subtree is not a valid BST return false.
9+
if not self.is_valid_bst(root.left):
10+
return False
11+
12+
# If current node's value is not greater than the previous
13+
# node's value in the in-order traversal return false.
14+
if self.previous and self.previous.val >= root.val:
15+
return False
16+
17+
# Update previous node to current node.
18+
self.previous = root
19+
20+
# If right subtree is not a valid BST return false.
21+
return self.is_valid_bst(root.right)
22+
23+
# Count nodes in current tree.
24+
def count_nodes(self, root: Optional[TreeNode]) -> int:
25+
if not root:
26+
return 0
27+
28+
# Add nodes in left and right subtree.
29+
# Add 1 and return total size.
30+
return 1 + self.count_nodes(root.left) + self.count_nodes(root.right)
31+
32+
def largestBSTSubtree(self, root: Optional[TreeNode]) -> int:
33+
if not root:
34+
return 0
35+
36+
# Previous node is initially null.
37+
self.previous = None
38+
39+
# If current subtree is a validBST, its children will have smaller size BST.
40+
if self.is_valid_bst(root):
41+
return self.count_nodes(root)
42+
43+
# Find BST in left and right subtrees of current nodes.
44+
return max(self.largestBSTSubtree(root.left), self.largestBSTSubtree(root.right))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /