Friday, July 17th, 2020 🤔
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
1. Recursion
Design a new helper function to help iterate left to right:
def helper(node, lower = float('-inf'), upper = float('inf')):
#‘inf’ means the infinite value
Approach:
As long as the left sub-tree is not None, and all the node.val in the left sub-tree is less than the root.val; if the right sub-tree is not none, then all the node.val in the right is bigger than the root.val.
2. In-order traversal
Approach:
Use a stack to store the nodes
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Example 2:
1. DP
return 0
2.1 if s[i-1] == ‘)’ : s[i-1 - dp[i-1]]
2.2 if s[i-1] == ')' and s[i-1-dp[i-1]] == '(' and i-1-dp[i-1]>=0:
2. Stack
Passed !