把三个二叉树遍历的题放在一起了。

递归写法太简单,就不再实现了,每题实现了两种非递归算法。

一种是利用栈,时间和空间复杂度都是O(n)。

另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点的空指针域。

先序:

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].

 class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> s;
if (root) {
s.push(root);
}
while (!s.empty()) {
TreeNode *p = s.top();
s.pop();
result.push_back(p->val);
if (p->right) {
s.push(p->right);
}
if (p->left) {
s.push(p->left);
}
}
return result;
}
};
 class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
while (p) {
if (!p->left) {
result.push_back(p->val);
p = p->right;
} else {
TreeNode *q = p->left;
while (q->right && q->right != p) {
q = q->right;
}
if (q->right == nullptr) {
result.push_back(p->val);
q->right = p;
p = p->left;
} else {
q->right = nullptr;
p = p->right;
}
}
}
return result;
}
};

中序:

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

 class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
stack<TreeNode *> s;
while (!s.empty() || p != nullptr) {
if (p != nullptr) {
s.push(p);
p = p->left;
} else {
p = s.top();
s.pop();
result.push_back(p->val);
p = p->right;
}
}
return result;
}
};
 class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
while (p) {
if (p->left) {
TreeNode *q = p->left;
while (q->right && q->right != p) {
q = q->right;
}
if (q->right == p) {
result.push_back(p->val);
q->right = nullptr;
p = p->right;
} else {
q->right = p;
p = p->left;
}
} else {
result.push_back(p->val);
p = p->right;
}
}
return result;
}
};

后序:

Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [3,2,1].

 class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> s;
TreeNode *p = root;
TreeNode *q = nullptr, *last = nullptr;
while (!s.empty() || p) {
if (p) {
s.push(p);
p = p->left;
} else {
q = s.top();
if (q->right && last != q->right) {
p = q->right;
} else {
result.push_back(q->val);
s.pop();
last = q;
}
}
}
return result;
}
};
 class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode dummy();
TreeNode *p = &dummy, *pre = nullptr;
p->left = root;
while (p) {
if (p->left == nullptr) {
p = p->right;
} else {
pre = p->left;
while (pre->right != nullptr && pre->right != p) {
pre = pre->right;
}
if (pre->right == nullptr) {
pre->right = p;
p = p->left;
} else {
printReverse(result, p->left, pre);
pre->right = nullptr;
p = p->right;
}
}
}
return result;
} private:
void printReverse(vector<int>& v, TreeNode* from, TreeNode *to) {
reverse(from, to);
TreeNode *p = to;
while (true) {
v.push_back(p->val);
if (p == from) {
break;
}
p = p->right;
}
reverse(to, from);
} void reverse(TreeNode *from, TreeNode *to) {
if (from == to) {
return;
}
TreeNode *x = from, *y = x->right, *z;
while (true) {
z = y->right;
y->right = x;
x = y;
y = z;
if (x == to) {
break;
}
}
}
};

【Leetcode】Binary Tree Traversal的更多相关文章

  1. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  2. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  4. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  5. 【leetcode】Binary Tree Level Order Traversal I & II

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  7. 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 【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 ...

  9. 【Leetcode】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 注意for循环中变量的作用域-乾颐堂

    1 2 for e in collections:     pass 在for 循环里, 最后一个对象e一直存在在上下文中.就是在循环外面,接下来对e的引用仍然有效. 这里有个问题容易被忽略,如果在循 ...

  2. Oracle——约束

    NOT NULLUNIQUE PRIMARY KEYFOREIGN KEYCHECK 如果不指定约束名 ,Oracle server 自动按照 SYS_Cn 的格式指定约束名 --指定约束名 CREA ...

  3. HDU 2602 Bone Collector (01背包DP)

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  4. 视频分析(MATLAB)——MV分镜头图像分类

    引言:一个MV视频是有很多帧图像组合而成的,而一支MV是有多少个分镜头场景组合而成的呢?由MATLAB如何自动实现? 以<Love You Like A Love Song>的MV为例(这 ...

  5. SQL语句统计错误率

    2018年的第一篇博客就以此作为开端吧 :D 最近在项目中碰到需要统计类似错误率之类的需求,原本这功能是之前做的,但是最近测的时候发现出了点问题,显示的结果不对.这就比较尴尬了... 于是就进行deb ...

  6. access函数使用

    调用open函数时,是以有效用户而不是实际用户的身份去验证进程对要打开的文件的读写权限.但是有时候我们想知道的是实际用户而非有效用户对某一文件的权限,此时就要用到access函数.   函数原型:in ...

  7. MongoDB整理笔记のID自增长

    以下是官网原文地址: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ 概要 MongoDB 的_i ...

  8. [WIN7]Win7建立AccessPoint(SoftAP)

    要构建可以访问互联网的无线接入点,必须有台带无线网卡(正常安装完驱动)的并且能访问Internet的计算机. 1.检查环境 [网络共享中心] - > [网络适配器] 检查有线无线网络适配器是否都 ...

  9. elasticsearch(0.90.10)安装配置+超多插件!!

    一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...

  10. selenium三大浏览器driver下载地址

    Chrome 点击下载chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的ch ...