题目:
给定一个二叉树,返回它的中序 遍历。
思路:
练习过很多次了,都记住了,但是没有找到用哈希表的点。使用递归来实现很方便,掌握中序遍历的原理就好。
程序:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
result = []
self.auxiliary(root, result)
return result
def auxiliary(self, root: TreeNode, result: List[int]):
if root:
self.auxiliary(root.left, result)
result.append(root.val)
self.auxiliary(root.right, result)