Lintcode66 Binary Tree Preorder Traversal solution 题解
来源:互联网 发布:tomcat部署java项目 编辑:程序博客网 时间:2024/05/22 04:56
【题目描述】
Given a binary tree, return the preorder traversal of its nodes' values.
给出一棵二叉树,返回其节点值的前序遍历。
【题目链接】
www.lintcode.com/en/problem/binary-tree-preorder-traversal/
【题目解析】
递归
递归版很好理解,首先判断当前节点(根节点)是否为null,是则返回空vector,否则先返回当前节点的值,然后对当前节点的左节点递归,最后对当前节点的右 节点递归。递归时对结果的处理方式不同可进一步细分为遍历和分治两种方法。
分治
使用分治的方法和递归类似,但是不同的是递归是将结果作为参数传入递归函数中,而分治则是先将结果保留,随后再合并到最终结果中。
迭代
迭代时需要利用栈来保存遍历到的节点,首先进行出栈抛出当前节点,保存当前节点的值,随后将右、左节点分别入栈,迭代到其为叶子节点(NULL)为止。
【参考答案】
www.jiuzhang.com/solutions/binary-tree-preorder-traversal/
阅读全文
0 0
- Lintcode66 Binary Tree Preorder Traversal solution 题解
- Binary Tree Preorder Traversal题解
- Binary Tree Preorder Traversal (Recursive solution)
- [LeetCode] Binary Tree Preorder Traversal, Solution
- Lintcode73 Construct Binary Tree from Preorder and Inorder Traversal solution 题解
- Lintcode73 Construct Binary Tree from Preorder and Inorder Traversal solution 题解
- LeetCode题解:Binary Tree Preorder Traversal
- LeetCode题解:Binary Tree Preorder Traversal
- LeetCode题解-144-Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal 题解
- Lintcode67 Binary Tree Inorder Traversal solution 题解
- Lintcode68 Binary Tree Postorder Traversal solution 题解
- Lintcode67 Binary Tree Inorder Traversal solution 题解
- Lintcode68 Binary Tree Postorder Traversal solution 题解
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal, Solution
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- hadoop-25
- Hibernate入门09_QBC及SQL查询
- 每天学点Linux之sed命令的使用
- java堆、栈、堆栈的区别
- oracle的行级触发器使用
- Lintcode66 Binary Tree Preorder Traversal solution 题解
- QImage 与 cv::Mat 之间的相互转换
- Java基础 快速学习(二)
- LNMP 搭建
- webjs -- jsonp的原理
- 闪回版本查询+闪回版本查询案例1+闪回版本查询案例2
- mstar面试题:把tv和radio节目排序,tv放前面,radio放后面。 节目类型相同的按频点又低到高排序
- 在大学上的日子
- 把Java程序打包成jar文件包并执行