题目描述
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
题目理解
代码实现
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result = []
stack = []
p = root
while p or stack:
# Save the nodes which have left child
while p:
stack.append(p)
p = p.left
if stack:
p = stack.pop()
# Visit the middle node
result.append(p.val)
# Visit the right subtree
p = p.right
return result