标签:遍历 log binary treenode root append end object obj
LeetCode: 144. Binary Tree Preorder Traversal
# 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 preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ret = []
stack = []
while root or stack:
while root:
ret.append(root.val)
stack.append(root)
root = root.left
if stack:
t = stack.pop()
root = t.right
return ret
标签:遍历 log binary treenode root append end object obj
原文地址:http://www.cnblogs.com/qiaojushuang/p/7862930.html