题解 | #二叉树的中序遍历#
二叉树的中序遍历
http://www.nowcoder.com/practice/0bf071c135e64ee2a027783b80bf781d
class Solution:
def inorderTraversal(self , root: TreeNode) -> List[int]:
# write code here
result = []
stack = []
curr = root # 根节点
while len(stack) != 0 or curr:
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
# print(curr.val,len(stack))
if curr.val != '#':
result.append(curr.val) # 左
curr = curr.right
return result
def inorderTraversal(self , root: TreeNode) -> List[int]:
# write code here
result = []
stack = []
curr = root # 根节点
while len(stack) != 0 or curr:
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
# print(curr.val,len(stack))
if curr.val != '#':
result.append(curr.val) # 左
curr = curr.right
return result