LeetCode 94.二叉树的中序遍历 Python题解
# coding=utf-8
# Creator:Mr.Zhao
# 二叉树的中序遍历
"""
给定一个二叉树的根节点root ,返回它的中序遍历。
"""
# 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 = right
class Solution:
def inorderTraversal(self, root: TreeNode):
WHITE, GRAY = 0, 1
res = []
stack = [(WHITE, root)]
while stack:
color, node = stack.pop()
if node is None: continue
if color == WHITE:
stack.append((WHITE, node.right))
stack.append((GRAY, node))
stack.append((WHITE, node.left))
else:
res.append(node.val)
return res
"""
本质上比较简单就不说了
"""