LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal
题目:树的先序和后序。
后序地址:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/
先序地址:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
后序算法:利用栈的非递归算法。初始时,先从根节点一直往左走到底,并把相应的元素进栈;在循环里每次都取出栈顶元素,如果该栈顶元素的右子树在上一次已经访问到,则将该元素存入vector中,并且记录下上一次访问的元素,否则,往右走一步,然后在一直往左走到底,并把相应的元素进栈。一直循环,直到栈顶结束。代码:
/**
* 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> postorderTraversal(TreeNode *root) {
vector<int> result;
if(!root) return result;
TreeNode *p = root;
stack<TreeNode*> stk;
while(p){
stk.push(p);
p = p->left;
}
TreeNode *pre = NULL;
while(!stk.empty()){
p = stk.top();
if(!p->right || p->right == pre){
pre = p;
result.push_back(p->val);
stk.pop();
}else{
p = p->right;
while(p){
stk.push(p);
p = p->left;
}
}
}
return result;
}
};
先序算法:利用栈的非递归算法。初始时,先从根节点一直往左走到底,并且把相应的元素进栈,以及把相应的元素存入vector。在循环里,每次取出栈顶元素,然后出栈一个元素,如果有右子树的话,往右走,然后一直往左走到底,并且把相应的元素进栈,以及把相应的元素存入vector。一直循环,直到栈为空。代码:
/**
* 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> result;
if(!root) return result;
stack<TreeNode*> stk;
TreeNode *p = root;
while(p){
result.push_back(p->val);
stk.push(p);
p = p->left;
}
while(!stk.empty()){
p = stk.top();
stk.pop();
if(p->right){
p = p->right;
while(p){
result.push_back(p->val);
stk.push(p);
p = p->left;
}
} }
return result;
}
};
LeetCode: Binary Tree Traversal的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] 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 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- 【剑指offer 面试题22】栈的压入、弹出序列
思路: 不停地压栈,直到栈头元素与弹出序列的首元素相等则出栈,同时弹出序列后移:若不相等则一直保持压栈,直到压入所有元素后弹出序列仍不为空,则说明无法匹配. C++: #include <ios ...
- (转载) VS编译duilib项目时候的错误解决方法整理
原文地址:http://blog.csdn.net/x356982611/article/details/30217473 @1:找不到Riched20.lib 用everything等软件搜索下磁盘 ...
- 【windows核心编程】使用远程线程注入DLL
前言 该技术是指通过在[目标进程]中创建一个[远程线程]来达到注入的目的. 创建的[远程线程]函数为LoadLibrary, 线程函数的参数为DLL名字, 想要做的工作在DLL中编写. 示意图如下: ...
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- CentOS 7 最小化安装之后安装Mysql
启动网卡 验证网络管理器和网卡接口状态 # systemctl status NetworkManager.service # nmcli dev status 修改网卡配置文件 通过上一步可以看到有 ...
- pku3668 Game of Lines
http://poj.org/problem?id=3668 水题,STL #include <stdio.h> #include <set> using namespace ...
- 阿里云slb http https配置
- mysql注册服务
http://www.2cto.com/database/201301/185456.html ____________________________________________________ ...
- hdu 3038 How Many Answers Are Wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...
- codeforces Ebony and Ivory(水题)
A. Ebony and Ivory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...