leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)
题目:
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)复杂度,时间O(n),空间O(n)
实现:
一、递归
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> root_vec;
vector<int> left_vec;
vector<int> right_vec;
if(root==NULL) return root_vec;
root_vec.push_back(root->val);
if(root->left!=NULL) left_vec=preorderTraversal(root->left);
if(root->right!=NULL) right_vec=preorderTraversal(root->right);
root_vec.insert(root_vec.end(),left_vec.begin(),left_vec.end());
root_vec.insert(root_vec.end(),right_vec.begin(),right_vec.end());
return root_vec;
}
};
二、非递归
根据先序遍历的顺序,先访问根节点,再访问左子树,后访问右子树,而对于每个子树来说,又按照同样的访问顺序进行遍历,非递归的实现思路如下:
对于任一节点P,
1)输出节点P,然后将其入栈,再看P的左孩子是否为空;
2)若P的左孩子不为空,则置P的左孩子为当前节点,重复1)的操作;
3)若P的左孩子为空,则将栈顶节点出栈,但不输出,并将出栈节点的右孩子置为当前节点,看其是否为空;
4)若不为空,则循环至1)操作;
5)如果为空,则继续出栈,但不输出,同时将出栈节点的右孩子置为当前节点,看其是否为空,重复4)和5)操作;
6)直到当前节点P为NULL并且栈空,遍历结束。
a、下面代码实现常规,和上面分析过程一致
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> preorder_vec;
TreeNode *p=root;//定义用来指向当前访问的节点的指针
if(p==NULL) return preorder_vec;//若为空树,则返回空vector
stack<TreeNode *> treenode_stack;//创建一个空栈
//直到当前节点p为NULL且栈空时,循环结束
while(p||!treenode_stack.empty())
{
//从根节点开始,输出当前节点,并将其入栈,
//同时置其左孩子为当前节点,直至其没有左孩子,及当前节点为NULL
preorder_vec.push_back(p->val);
treenode_stack.push(p);
p=p->left;
//如果当前节点p为NULL且栈不空,则将栈顶节点出栈,
//同时置其右孩子为当前节点,循环判断,直至p不为空
while(!p&&!treenode_stack.empty())
{
p=treenode_stack.top();
treenode_stack.pop();
p=p->right;
}
}
}
};
b、下面代码实现较简洁(个人感觉,不喜勿喷)
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode *> preorder_stack;
TreeNode *p=NULL;
vector<int> preorder_vec;
if(root==NULL) return preorder_vec;//若为空树,则返回空vector
preorder_stack.push(root);//当前节点入栈
while(!preorder_stack.empty())
{
p=preorder_stack.top();//栈顶节点出栈、输出
preorder_stack.pop();
preorder_vec.push_back(p->val);
//注意,下面入栈顺序不能错 ,因为先右后左,
//这样出栈时先遍历才是左孩子(左->中->右)
if(p->right) preorder_stack.push(p->right);//若存在右孩子,则入栈
if(p->left) preorder_stack.push(p->left);//若存在左孩子,则入栈
}
return preorder_vec;
}
};
leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)的更多相关文章
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
随机推荐
- VS2008注册码
PYHYP-WXB3B-B2CCM-V9DX9-VDY8T 如果下载的是90天的试用版,下载下来以后把ISO里面的Setup\setup.sdb文件用记事本打开,把其中的[Product Key]下面 ...
- hdoj 5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ----------- ...
- C#判断一个类中有无"指定名称"的方法
C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- jquery 选择器中含有空格注意
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- cocos2dx中加入unzip
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037323.html cocos2dx中没有直接解压文件的,自己网上找了个,记录一下. ...
- How To: Perl TCP / UDP Socket Programming using IO::Socket::INET
http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/ In this article, let us discuss ...
- OAB配置
OAB管理: http://blogs.technet.com/b/exchange_chs/archive/2013/01/31/exchange-server-2013-oab-managing- ...
- HDU1028 Ignatius and the Princess III 【母函数模板题】
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- nginx 配置http2
1.需要nginx 1.9.5+版本 2.需要ssl 证书 个人免费ssl 证书:https://buy.wosign.com/free/ 3.配置如下: server { listen ssl ht ...
- Task could not find "AxImp.exe" using the SdkToolsPath "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\"
本机v7.0A目录里没有AxImp.exe,无奈只能去官网下了个V7.1的. 安装完V7.1后,去“开始-所有程序-Microsoft Windows SDK v7.1”里找到Windows SDK ...