题目链接
https://leetcode.com/problems/binary-tree-inorder-traversal/
题目原文
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
return [1,3,2]
.
题目大意
中序遍历一棵二叉树
解题思路
递归求解,先遍历左子树,再遍历根节点,最后遍历右子树
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def inorderTraversal_re(self, root, inorder):
if not root:
return inorder
inorder = self.inorderTraversal_re(root.left, inorder)
inorder.append(root.val)
inorder = self.inorderTraversal_re(root.right, inorder)
return inorder
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
return self.inorderTraversal_re(root, [])