代码随想录08:二叉树

BBigSun 评论173阅读模式

代码随想录08:二叉树

理论基础

种类:文章源自十年又十年-https://www.bbigsun.com/415.html

  • 满二叉树
  • 完全二叉树
  • 二叉搜索树
  • 平衡二叉搜索树

存储:文章源自十年又十年-https://www.bbigsun.com/415.html

  • 链式存储(指针)
  • 顺序存储(数组)

遍历方式:文章源自十年又十年-https://www.bbigsun.com/415.html

深度优先遍历(DFS)文章源自十年又十年-https://www.bbigsun.com/415.html

  • 前序遍历(中左右)
  • 中序遍历(左中右)
  • 后续遍历(左右中)

广度优先遍历(BFS)文章源自十年又十年-https://www.bbigsun.com/415.html

  • 层次遍历

递归

  • (1)确定递归函数的参数和返回值
  • (2)确定终止条件
  • (3)确定单层递归的逻辑
res = []
def traversal(root):
    if root == None:
        return
    res.append(root.val)
    traversal(root.left)
    traversal(root.right)

traversal(root)
print(res)

翻转二叉树

思路:前序遍历,先交换左右节点,然后再遍历左右节点文章源自十年又十年-https://www.bbigsun.com/415.html 文章源自十年又十年-https://www.bbigsun.com/415.html

纸上得来终觉浅,绝知此事要躬行。

weinxin
17688689121
我的微信
微信扫一扫
BBigSun
  • 本文由 BBigSun 发表于 2023年 3月 31日 08:28:47
  • 转载请务必保留本文链接:https://www.bbigsun.com/415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定