|
| 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's value.</li> |
| 7 | + <li>The right subtree values are greater than the value of their parent (root) node's value.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p><strong>Note:</strong> A subtree must include all of its descendants.</p> |
| 11 | + |
| 12 | +<p> </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'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> </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> <= Node.val <= 10<sup>4</sup></code></li> |
| 35 | +</ul> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong>Follow up:</strong> Can you figure out ways to solve it with <code>O(n)</code> time complexity?</p> |
0 commit comments