您的位置:首页 > 其它

Leetcode #144 Binary Tree Preorder Traversal

2015-08-30 11:01 471 查看
Given a binary tree, return the preorder traversal of its nodes' values.

For example:

Given binary tree
{1,#,2,3}
,

1
\
2
/
3


return
[1,2,3]
.
Difficulty: Medium
学习到了Python 的global变量是怎么用的,CE了好多次,,
# 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 pT(self, root):
if(root is None):
return
ans.append(root.val)
self.pT(root.left)
self.pT(root.right)
return

def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
global ans
ans = []
self.pT(root)
return ans
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: