Kunlin Tan
Kunlin Tan
## Python 代码,附注释和时空复杂度 [236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) ```python class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': """ 解题思路:每个节点要知道什么、做什么:什么时候做 遍历or递归 要知道自己的子树里是否有这两个数字->递归 要做什么:返回自己子树是否有这两个数字->递归 什么时候做:后序遍历,传递子树信息...
### Python两种方法,附解释和代码 #### 前序遍历 ```python class Codec: def serialize(self, root): """ 用一个list记录,最后转为string导出:前序遍历,空节点计作N,然后用,连接 """ res = [] def dfs(node): if not node: res.append("N") return res.append(str(node.val)) dfs(node.left) dfs(node.right) dfs(root) return ",".join(res) def...