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 ...
随机推荐
- centos下apache安装后无法访问
2013.11.28遇到的问题: -------------------------------------- 一.centos下apache安装后无法访问 得查一下防火墙的问题 iptables添加 ...
- TcxDBVerticalGrid优秀的编辑控件
- CodeForces 709C Letters Cyclic Shift (水题)
题意:给定一个字符串,让你把它的一个子串字符都减1,使得总字符串字典序最小. 析:由于这个题是必须要有一个字串,所以你就要注意这个只有一个字符a的情况,其他的就从开始减 1,如果碰到a了就不减了,如果 ...
- 代码中设置excel自定义格式为[红色]的处理方法
有时候,excel的自定义格式设置时 ,会遇到需要设置为¥#,##0;[红色]¥-#,##0的格式. 其中会带一个颜色标记,但是如果这样的一句代码,放在英文版的Office里面,就失效了,因为英文版应 ...
- jpa 支持(sql)JDBC标准语句
=====================dao================================ package com.jb.xs.Dao.impl; import java.uti ...
- Java数据类型简单认识
Java是一种强类型编程语言,因而在声明变量的时候必须声明数据类型,java语言有基本数据类型和引用数据类型这两大数据类型,基本数据类型有8种分别是4种整型.2种浮点类型.1种用于Unicode表示字 ...
- ExtJs非Iframe框架加载页面实现
在用Ext开发App应用时,一般的框架都是左边为菜单栏,中间为tab页方式的显示区域.而tab页面大多采用的嵌入一个iframe来显示内容.但是采用iframe方式有一个很大的弊端就是每次在加载一个新 ...
- 二进制程序分析工具Pin在Windows系统中的安装和使用方法
这篇日志其实很弱智,也是因为换了新电脑,实验环境不全(当然,做这个实验我是在虚拟机里,因为接下来想拿些恶意代码的数据),所以这里记录一下在Windows下怎么安装和使用Pin这个程序分析领域最常用的工 ...
- DCEF3 相关资料
DCEF3 调用 js http://www.cnblogs.com/Delphi-Farmer/p/4103708.html interface uses ceflib;//其它 type //这里 ...
- CentOS6.5下安装MariaDB5.5.36
yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...