Lintcode 68 二叉树的后序遍历
2016-08-28 18:34
176 查看
描述:
给出一棵二叉树,返回其节点值的后序遍历。
样例:
给出一棵二叉树 {1,#,2,3},
返回 [3,2,1]
挑战:
你能使用非递归实现么?
代码:
给出一棵二叉树,返回其节点值的后序遍历。
样例:
给出一棵二叉树 {1,#,2,3},
返回 [3,2,1]
挑战:
你能使用非递归实现么?
代码:
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { /** * @param root: The root of binary tree. * @return: Postorder in vector which contains node values. */ public: vector<int> ans; vector<int> postorderTraversal(TreeNode *root) { // write your code here if(root != NULL) { postorderTraversal(root->left); postorderTraversal(root->right); ans.push_back(root->val); } return ans; } };
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- 文件遍历排序函数
- C#递归算法之分而治之策略
- Lua 学习笔记之C API 遍历 Table实现代码
- 有关数据库SQL递归查询在不同数据库中的实现方法
- C#中的递归APS和CPS模式详解
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#中遍历Hashtable的4种方法
- WinForm实现按名称递归查找控件的方法
- C#递归方法实现无限级分类显示效果实例
- 使用SqlServer CTE递归查询处理树、图和层次结构
- C#递归算法之打靶算法分析
- C#中的尾递归与Continuation详解
- Erlang中遍历取出某个位置的最大值代码
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C++实现图的邻接表存储和广度优先遍历实例分析
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- C#递归实现显示文件夹及所有文件并计算其大小的方法