思想:
1)借助栈
2)stack一直添加非空的叶子结点
3)stack每次pop()结点p,然后访问,最后p.right更新root
# 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):
stack = []
res = []
while root!=None or len(stack)>0:
while root!=None:
stack.append(root)
root = root.left
if len(stack)>0:
p = stack.pop()
res.append(p.val)
root = p.right
return res
网友评论