[leetcode]Binary Tree Postorder Traversal
来源:互联网 发布:嵌入式软件培训学校 编辑:程序博客网 时间:2024/06/05 09:56
题目:
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/
描述:二叉树的后序遍历,非递归
solution by python:
# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a list of integers def postorderTraversal(self, root): if not root: return [] stk,ans = [root,],[] pre = root while len(stk) != 0: p = stk[-1] if p.left==None and p.right==None or (p.left==pre or p.right==pre): ans.append(p.val) pre = stk.pop() else: if p.right != None: stk.append(p.right) if p.left != None: stk.append(p.left) return ans
0 0
- LeetCode Binary Tree Postorder Traversal
- LeetCode:Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- LeetCode:Binary Tree Postorder Traversal
- Leetcode Binary Tree Postorder Traversal
- [LeetCode]Binary Tree Postorder Traversal
- [LeetCode] Binary Tree Postorder Traversal
- leetcode Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Postorder Traversal
- LeetCode | Binary Tree Postorder Traversal
- Leetcode: Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal - LeetCode
- LeetCode - Binary Tree Postorder Traversal
- [LeetCode]Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal -- LeetCode
- [LeetCode]Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Postorder Traversal
- mysql视图和触发器
- 11.2.1.2 在 F# 中写单元测试
- java验证码识别--2
- 关于Oracle 的url 连接 最后一个orcl的理解
- NineOldAndroids:Android 3.0 Animation API的适配库
- [leetcode]Binary Tree Postorder Traversal
- java验证码识别--3
- Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class org.crazyit
- Java SE 6 新特性: Instrumentation 新功能
- C++和java技术特性对比
- RDIFramework.NET ━ Web中打印的各种方案参考-欢迎补充
- display:inline、block、inline-block的区别
- QComboBox设置Item行高
- google固定ip--程序员必备!