/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
1. 前序
1.1进出栈所有元素
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
// push-pop all nodes
vector<int> r;
stack<TreeNode*> s;
TreeNode* t;
while(root || !s.empty()){
if(root){
r.push_back(root->val);
s.push(root);
root = root->left;
}
else{
t = s.top();
s.pop();
root = t->right;
}
}
return r;
}
};
1.2进出栈所有右节点,1个节点进出两次
虽然不需要左节点压栈,但并不快,原因在于右节点进出栈了两次。
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
// push-pop only right nodes
vector<int> r;
stack<TreeNode*> s;
TreeNode* t;
while(root || !s.empty()){
if(root){
r.push_back(root->val);
s.push(root->right);
root = root->left;
}
else{
root = s.top();
s.pop();
}
}
return r;
}
};
2. 中序
2.1进出栈所有元素
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
// push-pop all nodes
vector<int> r;
stack<TreeNode*> s;
TreeNode* t;
while(root || !s.empty()){
if(root){
s.push(root);
root = root->left;
}
else{
t = s.top();
s.pop();
root = t->right;
r.push_back(t->val);
}
}
return r;
}
};
2.2 所有叶子保存后续节点,最后一个叶子除外。
每个节点保存在叶子需要遍历一次,之后打印时需再次遍历,即2(n-1)此,即O(n), 而非nlgn.参看下图
不破坏树的结构。
(Morris's traveral)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
// no stack, a node's left's right's right....right leaf's right save the postOrder element
vector<int> r;
TreeNode* p;
while(root){
if(NULL == root->left){
//no left node
r.push_back(root->val);
root = root->right;
}
else{
//has left node
p = root->left;
while(NULL != p->right && p->right != root){
//left's right's right....right leaf's right
p = p->right;
}
if(NULL == p->right){
p->right = root;
root = root->left;
}
else{
r.push_back(root->val);
root = root->right;
p->right = NULL;
}
}
}
return r;
}
};
2.3 recursion
class Solution {
vector<int> r;
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root) return r;
if(root->left) inorderTraversal(root->left);
r.push_back(root->val);
if(root->left) inorderTraversal(root->left);
return r;
}
};
3.后序
3.1. 区分在stack中是第一次还是第二次遍历,因先压栈右节点,此时可置右节点的父节点的右节点为NULL,右节点已进栈。
破坏了树的结构。
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> r;
stack<TreeNode*> s;
TreeNode* t;
map<TreeNode*,bool> m;
while(root || !s.empty()){
if(root){
s.push(root);
root = root->left;
}
else{
if(!s.empty()){
t = s.top();
if(!t->right){
// no right node
s.pop();
r.push_back(t->val);
root = NULL;
}
else{
if(m[t] == false) {
//have right (first time)
m[t] = true;
root = t->right;
}
else{
//have right (second time)
s.pop();
r.push_back(t->val);
root = NULL;
m.erase(t);
}
}
}
}
}
return r;
}
};
3.2 同3.1,只是用map区分第一第二次访问。效率大不如3.1,但没有改变树的结构。
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> r;
stack<TreeNode*> s;
TreeNode* t;
map<TreeNode*,bool> m;
while(root || !s.empty()){
if(root){
s.push(root);
root = root->left;
}
else{
if(!s.empty()){
t = s.top();
if(!t->right){
// no right node
s.pop();
r.push_back(t->val);
root = NULL;
}
else{
if(m[t] == false) {
//have right (first time)
m[t] = true;
root = t->right;
}
else{
//have right (second time)
s.pop();
r.push_back(t->val);
root = NULL;
m.erase(t);
}
}
}
}
}
return r;
}
};
Notes:
a. for 2.2 Morris's
b. for 前中后序比较通用的非递归做法
c. 对后序,可考虑双向链表,直接 [root, root->right, root->left]遍历,但插入双向链表的表头。
这种方法开发快,可读性好,好维护。