数组
链表
二叉树
class TreeNode:
def __init__(self,val = None,left = None,right = None):
self.val = val
self.left = left
self.right = right
class Solution:
c = 0
def inorder(self,root:TreeNode):
l = []
def f(root):
if root.left:f(root.left)
self.c += 1
l.append(root.val)
if root.right:f(root.right)
f(root)
print(self.c)
return l
if __name__ == '__main__':
t = TreeNode(2)
t0 = TreeNode(1,right=t)
t1 = TreeNode(4)
t2 = TreeNode(3,right = t1,left=t0)
print(Solution().inorder(t2))
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def recoverTree(self, root) -> None:
"""
Do not return anything, modify root in-place instead.
双指针 中序遍历二叉树,出现降序时记录第一个第二个降序点,结束交换
"""
f = None
s = None
pre = TreeNode(float("-inf"))
p = root
stack = []
while p or len(stack):
while p:
stack.append(p)
p = p.left
p = stack.pop()
if not f and pre.val > p.val:
f = pre
if f and pre.val > p.val:
s = p
pre = p
p = p.right
f.val, s.val = s.val, f.val
def isSymmetric(self, root) -> bool:
if not root: True
def helper(left, right):
if left is None and right is None:
return True
if left is not None and right is None:
return False
if left is None and right is not None:
return False
if left.val != right.val:
return False
else:
return helper(left.left, right.right) and helper(left.right, right.left)
return helper(root.left, root.right)
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def DFS(self, root):
if not root:return 0
l = self.DFS(root.left)
r = self.DFS(root.right)
return max(l, r) + 1
def BFS(self,root):
c = 0
import queue
if not root: return 0
q = queue.Queue()
q.put(root)
while q.empty():
for i in range(q.size()):
node = q.get()
print(node.val)
if node.lchild:
q.put(node.lchild)
if node.rchild:
q.put(node.rchild)
c += 1
return c
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
if not root:return
res = []
def bst(root):
if not root:return
bst(root.left)
res.append(root.val)
bst(root.right)
bst(root)
return res[k-1]
if __name__ == '__main__':
Solution().kthSmallest()
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if (root.val - p.val) * (root.val - q.val) <= 0:
return root
if root.val > p.val:
return self.lowestCommonAncestor(root.left, p, q)
else:
return self.lowestCommonAncestor(root.right, p, q)
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
ans = 0
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def dfs(root,p,q):
if not root:return False
lson = dfs(root.left, p, q)
rson = dfs(root.right, p, q)
if (lson and rson) or ((root.val == p.val or root.val == q.val) and (lson or rson)):
self.ans = root
return lson or rson or (root.val == p.val or root.val == q.val)
dfs(root, p, q)
return self.ans
栈与队列
字符串
排序
动态规划
分治法
贪心
滑动窗口
|