Binary Tree Postorder Traversal - Leetcode
来源:互联网 发布:阿里云下载 编辑:程序博客网 时间:2024/06/04 18:22
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> postorderTraversal(TreeNode root) {Stack<TreeNode> s = new Stack<>();List<Integer> result = new ArrayList<>();TreeNode pointer = root, prev = null;do {while (pointer != null) {s.push(pointer);pointer = pointer.left;}prev = null;while (!s.empty()) {pointer = s.pop();if (pointer.right == prev) {result.add(pointer.val);prev = pointer;} else {s.push(pointer);pointer = pointer.right;break;}}} while (!s.empty());return result;}}
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]
.
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
- 智能背包核心硬件蓝牙ibeacon使用场景分析
- Binary Tree Preorder Traversal - Leetcode
- ios 日期转换详细
- UVA - 11210 Chinese Mahjong 枚举
- Metadata Locking在5.5里所做改动带来的影响
- Binary Tree Postorder Traversal - Leetcode
- 线段树基础 poj2104
- 动画
- 日期转换
- 六种常见的排序方法
- Eclipse常用快捷键
- VS2010出现"PCH 警告:标头停止点不能位于宏或#if块中"异常的解决方法
- Unicode和多字节
- jquery返回顶部