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 ...
随机推荐
- Cisco SDM
SDM连接方式:http+telnet / https+ssh 要使用SDM对CISCO设备实现集中式管理,必须在设备上键入如下命令: 步骤1: 要启用路由器的HTTP/HTTPS 服务器,请 ...
- LeetCode100:Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- web.xml filter 顺序
The order the container uses in building the chain of filters to be applied for a particular request ...
- C#取得当前目录 转载
/获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssem ...
- ActiveMQ的消息确认问题
http://riddickbryant.iteye.com/blog/441890 [发送端] session = connection.createSession(Boolean.FALSE, ...
- Flex 日期和字符串之间转换
字符串转为日期: var dateTime:Date= DateField.stringToDate(deTime, "YYYY-MM-DD");//"YYYY-MM-D ...
- DISCUZ X2更换域名注意事项
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Error loading: \Java\jdk1.6.0_35\jre\bin\server\jvm.dll
先看看错误:complie: [exec] Error loading: D:\Program Files\Java\jdk1.6.0_35\jre\bin\server\jvm.dll [exec] ...
- __ARM_PROFILE_M__ __CORE__ __ARMVFP__ __LITTLE_ENDIAN__
__ARM_PROFILE_M__ Description An integer that is set based on the --cpu option. The symbol is set to ...
- WEB标准系列-HTML元素嵌套
转:http://www.smallni.com/element-nesting/ 先来看以下这样一段代码: <ul> <li><h4><a href=&qu ...