Binary Tree Preorder Traversal
来源:互联网 发布:mac什么游戏好玩 编辑:程序博客网 时间:2024/06/06 17:17
题目描述:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
大意:前序遍历二叉树,二叉树的基本操作。
解题思路:1,首先将root入栈,循环过程中,读取栈顶元素,pop,然后分别将右子树,左子树分别入栈。循环直至栈空
2,边读取,边入栈的特点。
上代码:
画图分析:
代码分析:1,递归算法中,比较明朗,但是注意调用的两参数函数,因为函数本身有返回值,所以递归的过程中要添加一个容器,且是引用,这样才能改变实参。
2,递归之前先将根节点入栈,循环条件为栈空。特点是边入栈(右子树,左子树),边读取(pop栈顶元素)
0 0
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Linux CentOS 下安装谷歌浏览器(Chromium)
- 自动检测iOS网络并可跳转至设置界面设置网络
- 命令mkdir -p
- GridView显示数据鼠标悬停变色
- java 获取表结构
- Binary Tree Preorder Traversal
- iOS 内存管理机制(详细解说)
- ASP.NET MVC4 入门(五)——控制器访问模型数据
- QT 继承自QGraphicsItem
- 自己经常浏览的一些牛人博客
- HDU 2149 Public Sale (博弈论经典)
- 使用codeReview工具时,消除一些空格格式错误警告的eclipse相关配置
- leetcode: Pow(x, n)
- 单向链表的创建 输入 输出