LintCode 68. Binary Tree Postorder Traversal
程序员文章站
2024-03-24 14:26:16
...
题目
思路
非递归后序遍历。
代码
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param: root: A Tree
@return: Postorder in ArrayList which contains node values.
"""
def postorderTraversal(self, root):
# write your code here
res_list = []
stack = []
lastNode = root
if root: stack.append(root)
while stack:
node = stack[-1]
if node.left and lastNode != node.left and lastNode != node.right:
stack.append(node.left)
elif node.right and lastNode != node.right:
stack.append(node.right)
else:
res_list.append(node.val)
stack.pop(-1)
lastNode = node
return res_list
下一篇: 07. Linux 的命令分类
推荐阅读
-
LintCode 68. Binary Tree Postorder Traversal
-
LeetCode:102.Binary Tree Level Order Traversal
-
leecode每日一题01- Binary Tree Level Order Traversal [Medium]
-
Binary Tree Postorder Traversal
-
LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)
-
LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
-
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal
-
leetcode 随笔 Construct Binary Tree from Preorder and Inorder Traversal
-
105. Construct Binary Tree from Preorder and Inorder Traversal
-
Construct Binary Tree from Preorder and Inorder Traversal