Binary Tree Postorder Traversal
来源:互联网 发布:向日葵虚拟局域网软件 编辑:程序博客网 时间:2024/06/05 07:18
题目描述:
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
大意:后续遍历二叉树
解题思路:
1,与其他遍历方式不同,后续遍历是最为麻烦的,因为栈内元素出栈需要条件(节点指针左右子树都为空,或者该节点的左子树或者右子树刚才已经弹出(刚才弹出的不能为空))
2,所有元素都必须先入栈
代码部分:
图示:
代码解析:
1,大部分都先入栈了,只是后来碰到了根节点,不满足弹出条件,接着将右子树入栈。
2,代码中的pre不为空,一定要判断,因为pre本身的意思是刚才弹出的元素。如果不加该条件,那么pre为空,可以为根节点的左子树(左子树为空的特殊二叉树),就会将1弹出了,不再是后续遍历了。
0 0
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- 说说大型高并发高负载网站的系统架构(更新)
- NOJ1090Rock, Paper, Scissors——水题
- cocos2d-x-2.2.0_win7+vs2010搭建_eclipse+ndk-r9
- 从入门到精通: 最小费用流的“zkw算法”
- 2013年成都邀请赛解题报告
- Binary Tree Postorder Traversal
- NOJ1091Black Vienna——人工智能?No...No...No...二分+枚举
- 水風井
- 刚接触开发板之接口接线工具
- windows下 wxPython 无法导入的解决方法
- 【比特科技】计算机专业就业指南(一)
- 【Nginx】事件驱动框架和异步处理
- UI笔记_UIPageControl
- linux/unix man用法