leetcode [python] 【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]
"""
if root == None:
return []
return [root.val] + (self.preorderTraversal(root.left)) + (self.preorderTraversal(root.right))
思路二:非遞迴
使用資料結構棧,相當於層次遍歷的思路。
# 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]
"""
res = []
top = root
stack =[]
while top or stack:
if top == None:
top = stack.pop()
res.append(top.val)
if top.right:
stack.append(top.right)
top = top.left
return res
相關文章
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- LeetCode144:Binary Tree Preorder TraversalLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- 【LeetCode 144_二叉樹_遍歷】Binary Tree Preorder TraversalLeetCode二叉樹
- Leetcode Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Construct Binary Tree from Preorder and Inorder Traversal leetcode javaStructLeetCodeJava
- Construct Binary Tree from Preorder and Inorder TraversalStruct
- Leetcode 105 Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- LeetCode-Verify Preorder Serialization of a Binary TreeLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- Leetcode Binary Tree Level Order Traversal IILeetCode
- Leetcode-Binary Tree Level Order TraversalLeetCode
- Binary Tree Level Order Traversal leetcode javaLeetCodeJava
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)LeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode145:Binary Tree Postorder TraversalLeetCode
- Leetcode Binary Tree Zigzag Level Order TraversalLeetCode
- Leetcode-Binary Tree Level Order Traversal IILeetCode
- Binary Tree Level Order Traversal II leetcode javaLeetCodeJava
- Leetcode-Construct Binary Tree from inorder and preorder travesalLeetCodeStruct
- Leetcode-145題:Binary Tree Postorder TraversalLeetCode
- Leetcode Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct