85. 最大矩形
94. 二叉树的中序遍历
简单题
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root: return []
res = []
def dfs(root, res):
if not root: return
if root.left:
dfs(root.left, res)
res.append(root.val)
if root.right:
dfs(root.right, res)
dfs(root, res)
return res
96. 不同的二叉搜索树
不会
const numTrees = (n) => {
const dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
for (let j = 0; j <= i - 1; j++) {
dp[i] += dp[j] * dp[i - j - 1];
}
}
return dp[n];
};
照着答案写的:
class Solution:
def numTrees(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
for i in range(2, n + 1):
for j in range(i):
if j <= i - 1:
dp[i] += dp[j] * dp[i - 1 - j]
return dp[n]
第二遍:
写出来了,我觉得要把边界情况想明白并不容易。
class Solution:
def numTrees(self, n: int):
dp = [0] * (n + 1)
dp[0] = dp[1] = 1
for i in range(2, n + 1):
for j in range(0, i):
dp[i] += dp[j] * dp[i - 1 - j]
return dp[-1]
98. 验证二叉搜索树
最简单的想法就是dfs遍历,然后如果是升序的数组,就是BST。但是遇到了[2, 2, 2]然后发现过不了,加了一个set就过了。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
if not root: return
res = []
def dfs(root):
if not root:
return
if root.left:
dfs(root.left)
res.append(root.val)
if root.right:
dfs(root.right)
dfs(root)
return res == sorted(set(res))
答案:
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
stack, inorder = [], float('-inf')
while stack or root:
while root:
stack.append(root)
root = root.left
root = stack.pop()
# 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
if root.val <= inorder:
return False
inorder = root.val
root = root.right
return True
第二遍:还是没写对
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root) -> bool:
def dfs(root):
if not root:
return True
if root.left:
if root.left.val > root.val:
return False
return dfs(root.left)
if root.right:
if root.right.val < root.val:
return False
return dfs(root.right)
return True
if root.left:
if root.left.val >= root.val:
return False
if root.right:
if root.right.val <= root.val:
return False
return dfs(root.left) and dfs(root.right)
看到了一个答案:
官方答案:
按照答案写出来的,来需要在看。。。。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root):
def dfs(root, lower, upper):
if not root:
return True
val = root.val
if lower != None and root.val <= lower:
return False
if upper != None and root.val >= upper:
return False
if not dfs(root.left, lower, val): return False
if not dfs(root.right, val, upper): return False
return True
return dfs(root, None, None)
101. 对称二叉树
尝试写了一下,没写对。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def helper(root):
if not root.left and not root.right:
return
if root.left and root.right:
if root.left.val == root.right.val:
return helper(root.left) and helper(root.right)
else:
return False
else:
return False
return helper(root)
下面是答案:
class Solution {
public:
bool check(TreeNode *p, TreeNode *q) {
if (!p && !q) return true;
if (!p || !q) return false;
return p->val == q->val && check(p->left, q->right) && check(p->right, q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root, root);
}
};
根据答案写的:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root: return True
def dfs(root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
return root1.val == root2.val and dfs(root1.left, root2.right) and dfs(root1.right, root2.left)
return dfs(root.left, root.right)
想了BFS是可以实现的,DFS,还是不会写。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root):
def preorder(root, flag, pth):
if not root:
return pth[:]
pth.append(root.val)
if flag:
if root.left:
preorder(root.left, flag, pth)
if root.right:
preorder(root.right, flag, pth)
else:
if root.right:
preorder(root.right, flag, pth)
if root.left:
preorder(root.left, flag, pth)
pth1 = preorder(root.left, True, [])
pth2 = preorder(root.right, False, [])
print(pth1)
print(pth2)
看答案写了一遍,是觉得真简单,但是自己想不到。。。。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root):
def dfs(root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
if root1.val != root2.val:
return False
return dfs(root1.left, root2.right) and dfs(root1.right, root2.left)
return dfs(root.left, root.right)