第四步判断是否含循环必不可少,要根据题目要求来处理。除非L310 min height明确一定有解,而L269外星人字典就明确可能无解
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
deftopological_sort(self, graph: List[List[int]], n: int) -> List[int]: in_degree = [0] * n for i inrange(len(graph)): for node in graph[i]: in_degree[node] += 1
start_nodes = [i for i inrange(len(in_degree)) if in_degree[i] == 0] queue, res = deque(start_nodes), [] while queue: node = queue.popleft() res.append(node) for neighbor in graph[node]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: queue.append(neighbor) return res iflen(res) == n elseNone
definsert(self, word: str) -> None: it = self.head for i inrange(len(word)): # if word[i] not in it.children: #it.children[word[i]] = TrieNode() it = it.children[word[i]] it.is_end = True
defsearch(self, word: str) -> bool: it = self.head for i inrange(len(word)): if word[i] notin it.children: returnFalse it = it.children[word[i]] return it.is_end
defstartsWith(self, prefix: str) -> bool: it = self.head for i inrange(len(prefix)): if prefix[i] notin it.children: returnFalse it = it.children[prefix[i]] returnTrue
definsert(self, word: str) -> None: ifnot word: return it = self.head for i inrange(len(word)): # if word[i] not in it.children: # it.children[word[i]] = TrieNode() it = it.children[word[i]] if i == len(word) - 1: it.is_end = True
defsearch(self, word: str) -> bool: ifnot word: returnFalse it = self.head for i inrange(len(word)): if word[i] notin it.children: returnFalse it = it.children[word[i]] if i == len(word) - 1and it.is_end: returnTrue returnFalse
defstartsWith(self, prefix: str) -> bool: ifnot prefix: returnFalse it = self.head for i inrange(len(prefix)): if prefix[i] notin it.children: returnFalse it = it.children[prefix[i]] if i == len(prefix) - 1: returnTrue returnFalse
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6 Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 Output: 2 Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1 Output: 2
Constraints:
The number of nodes in the tree is in the range [2, 10<sup>5</sup>].
-10<sup>9</sup> <= Node.val <= 10<sup>9</sup> All Node.val are unique.
p != q * p and q will exist in the BST.
题目大意:
BST中求给定的两节点的最低共同父亲节点
解题思路:
三种情况,也是用DFS
解题步骤:
N/A
注意事项:
pq一定存在,所以有**三种情况: 1) p或q是root,另一是其子孙。 2) p,q分列root两边。 3) p,q在root的一边。跟LeetCode 236 Lowest Common Ancestor of a Binary Tree不同的是, 第二种情况,不用递归即知道,因为这是BST。第一和第三种情况同
第二种情况由于要比较p, q, root顺序,所以要令p, q有序,Line 4-5
Python代码:
1 2 3 4 5 6 7 8 9 10 11
deflowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': ifnot root: returnNone if p.val > q.val: # remember returnself.lowestCommonAncestor(root, q, p) if p.val <= root.val <= q.val or p == root or q == root: # remember root is p or q return root if p.val < root.val and q.val < root.val: returnself.lowestCommonAncestor(root.left, p, q) else: returnself.lowestCommonAncestor(root.right, p, q)