Leetcode-145題:Binary Tree Postorder 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 postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
res = []
p = root
visited = None
while p!=None or len(stack)!=0:
while p!= None:
stack.append(p)
p = p.left
p = stack[-1]
if p.right==None or visited==p.right:
p = stack.pop()
visited = p
res.append(p.val)
p = None
else:
p = p.right
return res
相關文章
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Construct Binary Tree from Inorder and Postorder TraversalStruct
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode145:Binary Tree Postorder TraversalLeetCode
- Leetcode Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)LeetCode
- Leetcode 106. Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- Construct Binary Tree from Preorder and Inorder TraversalStruct
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- 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
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- LeetCode144:Binary Tree Preorder 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 postorder travesalLeetCodeStruct
- leetcode [python] 【144】 Binary Tree Preorder TraversalLeetCodePython
- Leetcode Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Leetcode-binary Tree Zigzag Level Order TraversalLeetCode