94. 二叉树的中序遍历 --递归
1)中序遍历:左子树--父亲节点--右子树
2)二叉树核心
当前节点: root.val左子树:root.left右子树:root.right
3)二次函数定义栏中不需要写self
递归法:
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution:def inorderTraversal(self, root: TreeNode) -> List[int]:# 中序 :左子树-父亲节点--右子树def compute(root:TreeNode) :if not root :return None# 其实就是compute函数遍历到每个节点,该节点没有左子树了,就用 res.append(root.val) 解决自己。但是有层级区别# 例如完全二叉树 1-2-3(1是根节点)compute(root.left) # 一直往左走, 解决左子树 --- 解决2res.append(root.val) #当往左走不动时(没有左子孙后),加入该节点--- 解决1compute(root.right) # 一直往右走, 解决右子树res = []compute(root)return res
