C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal
提交网址: https://leetcode.com/problems/binary-tree-preorder-traversal/
Total Accepted: 118355 Total Submissions: 297596 Difficulty: Medium
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
分析:
借助栈实现非递归先序遍历算法的方法如下:
1)将二叉树的根结点作为当前结点。
2)若当前结点非空,则先访问该结点,并将该结点进栈,再将其左孩子结点作为当前结点,重复步骤2),直到当前结点为NULL为止。
3)若栈非空,则栈顶结点出栈,并将当前结点的右孩子结点作为当前结点。
4)重复步骤2)、3),直到栈为空且当前结点为NULL为止。
AC代码:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int x): val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> res;
TreeNode *p;
stack<TreeNode*> s;
p=root;
while(!s.empty() || p!=NULL)
{
if(p!=NULL)
{
res.push_back(p->val);
s.push(p);
p=p->left;
}
if(p==NULL)
{
p=s.top();
s.pop();
p=p->right;
}
}
return res;
}
};
// 以下为测试部分
/*
int main()
{
Solution sol;
vector<int> res;
TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
res=sol.preorderTraversal(root);
for(int i:res)
cout<<i<<" "; // 此处为vector遍历的方法,C++11标准支持
return 0;
}
*/
C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)的更多相关文章
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
随机推荐
- delphi fastreport 动态加载图片
(frxReport1.FindObject('picture1') as TfrxPictureView).Picture.LoadFromFile('d:\c.jpg'); frxReport1. ...
- hadoop教程
http://www.yiibai.com/hadoop/hadoop_enviornment_setup.html 改网站讲解详细,还有源码,值得借阅
- C++ 使用命名规范
刚开始正式学习C++, 之前写了一个C++ 的小程序,但是并没有注意命名规范之类的.这一次重写一个类似的程序,再加上这几天学习 c++Prime(发现好喜欢这本书.虽然看的很慢,每一小节都感是满满的干 ...
- vue 图片下载到本地,图片保存到本地
必须同源(访问的网站域名与服务器域名一致)才能下载 downs() { var alink = document.createElement("a"); alink.href = ...
- WPF一组Radio与enum绑定
工作中用到了一组RadioButton对应一组数据的情况,这个时候最好能将一组RadioButton绑定Enum. 步骤 1.MyControl库中Type.cs中定义Enum namespace M ...
- <c:forEach>循环列表,获取勾选的checkbox中某个<td>的值
<table> <!--列表表头 开始 --> <tr> <th><input type="checkbox" name=&q ...
- golang中数组指针和指针数组的区别
func test(){ x,y := 1, 2 var arr = [...]int{5:2} //数组指针 var pf *[6]int = &arr //指针数组 pfArr := [. ...
- NIOS II 之串口学习
UART中有6个寄存器分别为control, status, rxdata, txdata, divisor,endofpacket. 的寄存器是16位位宽的. UART会产生一个高电平的中断,当接收 ...
- 写给笨蛋徒弟的学习手册(3)—C#中15个预定义数据类型
在C#中学习中,你会很早的遇到预定义数据类型这个概念,但你有没有仔细想过它存在的意义?正所谓“存在即合理”,预定义数据类型的存在目的主要有俩个方面,一是为了增加程序的安全性,同时减轻编译器负担,加快编 ...
- xtrabackup单表备份与恢复
官网最新版本下载地址 https://www.percona.com/downloads/XtraBackup/LATEST/ yum install percona-xtrabackup; [epe ...