LintCode Q66 Binary Tree Preorder Traversal recursive in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年11月15日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: The root of binary tree.
@return: Preorder in ArrayList which contains node values.
"""
def preorderTraversal(self, root):
# write your code here
res = []
self.preOrder(root, res)
return res
def preOrder(self, root, res):
if root is None:
return
res.append(root.val)
self.preOrder(root.left, res)
self.preOrder(root.right, res)
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。