tree.py 456 B

123456789101112131415161718192021
  1. class TreeNode:
  2. def __init__(self, val=0, left=None, right=None):
  3. self.val = val
  4. self.left = left
  5. self.right = right
  6. # 回溯通用模板
  7. # res = []
  8. #
  9. # def backtrack(路径, 选择列表):
  10. # if 满足结束条件:
  11. # res.append(路径)
  12. # return
  13. #
  14. # if 满足剪枝条件: return
  15. #
  16. # for 选择 in 选择列表:
  17. # 做选择
  18. # backtrack(路径, 选择列表)
  19. # 撤销选择