2022年7月2日

LeetCode236 二叉树的最近公共祖先

摘要: LeetCode236 二叉树的最近公共祖先 dfs二叉树,记录以当前节点为根节点的子树是否包含p节点或q节点 记录第一次满足条件的节点即为所求最近公共祖先 判断条件为:1. 左右子树分别含有p节点和q节点;2. 当前节点为p节点或q节点,另一节点在左子树或右子树中. # Definition fo 阅读全文

posted @ 2022-07-02 16:28 solvit 阅读(25) 评论(0) 推荐(0)

LeetCode103 二叉树的锯齿形层序遍历

摘要: LeetCode103 二叉树的锯齿形层序遍历 使用两个栈进行模拟 + bfs # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # s 阅读全文

posted @ 2022-07-02 14:47 solvit 阅读(26) 评论(0) 推荐(0)

LeetCode33 搜索旋转排序数组(二分)

摘要: LeetCode33 搜索旋转排序数组 二分数列,仅仅当对于当前处理部分nums[l] < target < nums[r]时,进行答案更新,否则在另一部分寻找答案 class Solution: def search(self, nums: List[int], target: int) -> i 阅读全文

posted @ 2022-07-02 14:35 solvit 阅读(26) 评论(0) 推荐(0)

LeetCode386 字典序排数

摘要: LeetCode386 字典序排数 构造字典树,dfs遍历记录 class Solution: def lexicalOrder(self, n: int) -> List[int]: ans = [] def dfs(cur): if cur <= n: ans.append(cur) else: 阅读全文

posted @ 2022-07-02 14:17 solvit 阅读(24) 评论(0) 推荐(0)

导航