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

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

一种是利用栈,时间和空间复杂度都是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. Mysql(Navicat for Mysql)怎么添加数据库

    1.首先打开Navicat for Mysql: 2.打开后界面如下图所示,双击连接localhost_3306: 3.连接后localhost_3306变成绿色,如下图所示: 4.选中下面任意数据库 ...

  2. [c++] final override keyword

    the two keyword are aimed at virtual function final final function must be a virtual funtion , final ...

  3. 高性能python编程之协程(stackless)-乾颐堂

    我们都知道并发(不是并行)编程目前有四种方式,多进程,多线程,异步,和协程. 多进程编程在python中有类似C的os.fork,当然还有更高层封装的multiprocessing标准库,在之前写过的 ...

  4. 【转】The most comprehensive Data Science learning plan for 2017

    I joined Analytics Vidhya as an intern last summer. I had no clue what was in store for me. I had be ...

  5. 案例研究:手机APP的UI设计流程

    以下内容由Mockplus(http://www.mockplus.cn)团队翻译整理,仅供学习交流. UI设计——不仅仅是创造漂亮的图像. 面临的挑战 我为自己提供了一个绝佳的机会来训练我的视觉设计 ...

  6. Win 7 Windows Update无法自动更新解决方案

    最近发现系统很长时间没有自动更新过了,手动更新后,提示返回错误码WindowsUpdate_8024402F.网络上搜索到的解决方法大多是删除更新临时目录,重启WINDOWS UPDATE服务,然而试 ...

  7. Webstorm 10.0.4 配置

    1. 更换为sublime text的keymaps: https://github.com/ekaragodin/idea-sublime-keymap  (idea-sublime-keymap- ...

  8. sql查询层级分类

    先上个效果图吧 CTE递归查询里面用了一些小的技巧,查询出结果以后在前端用表格展示出来,层级视觉效果还是很明显的 with tree as(select [ID],[Name],[Address],[ ...

  9. python操作mysql数据库系列-安装MySQLdb

    一波三折,先是pip命令出现问题,然后各种方法尝试解决.然后是直接使用pip2命令安装报错,mysql-python库安装再次出现问题.于是使用国内镜像的方式去安装:pip2 install MySQ ...

  10. jmeter监控内存,CPU等方法

    方法1: 使用插件来监控CPU,内存等的使用情况 1.需要的插件准备 JMeterPlugins-Standard-1.4.0.zip , JMeterPlugins-Extras-1.4.0.zip ...