Leetcode: Binary Tree Inorder Traversal

来源:互联网 发布:oracle数据库免费下载 编辑:程序博客网 时间:2024/05/29 08:22

Question

Given a binary tree, return the inorder traversal of its nodes′ values.

For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

Hide Tags Tree Hash Table Stack


Analysis


Solution

# Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    # @param {TreeNode} root    # @return {integer[]}    def inorderTraversal(self, root):        return self.helper(root)    def helper(self,root):        if root==None:            return []        return self.helper(root.left) + [root.val] + self.helper(root.right)
0 0
原创粉丝点击