|
1 |
| -# Longest Mountain in Array |
2 |
| -Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: |
| 1 | +# Validate Binary Search Tree |
3 | 2 |
|
4 |
| -B.length >= 3 |
5 |
| -There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] |
6 |
| -(Note that B could be any subarray of A, including the entire array A.) |
| 3 | +Solution |
| 4 | +Given the root of a binary tree, determine if it is a valid binary search tree (BST). |
7 | 5 |
|
8 |
| -Given an array A of integers, return the length of the longest mountain. |
| 6 | +A valid BST is defined as follows: |
9 | 7 |
|
10 |
| -Return 0 if there is no mountain. |
| 8 | +The left subtree of a node contains only nodes with keys less than the node's key. |
| 9 | +The right subtree of a node contains only nodes with keys greater than the node's key. |
| 10 | +Both the left and right subtrees must also be binary search trees. |
| 11 | + |
11 | 12 |
|
12 | 13 | Example 1:
|
13 | 14 |
|
14 |
| -Input: [2,1,4,7,3,2,5] |
15 |
| -Output: 5 |
16 |
| -Explanation: The largest mountain is [1,4,7,3,2] which has length 5. |
| 15 | + |
| 16 | +Input: root = [2,1,3] |
| 17 | +Output: true |
17 | 18 | Example 2:
|
18 | 19 |
|
19 |
| -Input: [2,2,2] |
20 |
| -Output: 0 |
21 |
| -Explanation: There is no mountain. |
22 |
| -Note: |
23 | 20 |
|
24 |
| -0 <= A.length <= 10000 |
25 |
| -0 <= A[i] <= 10000 |
26 |
| -Follow up: |
| 21 | +Input: root = [5,1,4,null,null,3,6] |
| 22 | +Output: false |
| 23 | +Explanation: The root node's value is 5 but its right child's value is 4. |
| 24 | + |
| 25 | + |
| 26 | +Constraints: |
27 | 27 |
|
28 |
| -Can you solve it using only one pass? |
29 |
| -Can you solve it in O(1) space?<br> |
| 28 | +The number of nodes in the tree is in the range [1, 104]. |
| 29 | +-231 <= Node.val <= 231 - 1<br> |
30 | 30 |
|
31 | 31 | ## Idea
|
32 | 32 |
|
33 | 33 | ## Code
|
34 | 34 | ```python
|
| 35 | +# Definition for a binary tree node. |
| 36 | +# class TreeNode: |
| 37 | +# def __init__(self, val=0, left=None, right=None): |
| 38 | +# self.val = val |
| 39 | +# self.left = left |
| 40 | +# self.right = right |
35 | 41 | class Solution:
|
36 |
| - def longestMountain(self, A: List[int]) -> int: |
37 |
| - total = 0 |
38 |
| - |
39 |
| - length = len(A) |
40 |
| - if length <= 2: |
41 |
| - return 0 |
| 42 | + def isValidBST(self, root: TreeNode) -> bool: |
42 | 43 |
|
43 |
| - pointer = 0 |
44 | 44 |
|
45 |
| - while pointer <= length-2: |
46 |
| - base = pointer |
47 |
| - while pointer + 1 < length and A[pointer] < A[pointer+1]: |
48 |
| - pointer += 1 |
| 45 | + def preorder(root, left, right): |
| 46 | + if not root: |
| 47 | + return True |
| 48 | + if not left < root.val < right: |
| 49 | + return False |
49 | 50 |
|
50 |
| - if pointer == base: |
51 |
| - pointer += 1 |
52 |
| - continue |
53 |
| - |
54 |
| - peak = pointer |
55 |
| - while pointer + 1 < length and A[pointer] > A[pointer+1]: |
56 |
| - pointer += 1 |
57 |
| - |
58 |
| - if peak == pointer: |
59 |
| - continue |
60 |
| - |
61 |
| - total = max(total, pointer - base + 1) |
| 51 | + return (preorder(root.left, left, root.val) and preorder(root.right, root.val, right)) |
| 52 | + |
| 53 | + return preorder(root, float("-inf"), float("inf")) |
62 | 54 |
|
63 |
| - return total |
64 | 55 |
|
65 | 56 | ```
|
66 | 57 |
|
0 commit comments