【Leetcode】Binary Tree Traversal
把三个二叉树遍历的题放在一起了。
递归写法太简单,就不再实现了,每题实现了两种非递归算法。
一种是利用栈,时间和空间复杂度都是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的更多相关文章
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 【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 ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【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, ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【题解】【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, ...
- 【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 Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- Ubuntu 安装QT5 后编译程序报错: FindQt5Widgets.cmake
安装QT5.4后,需要编译一个C++程序. Cmakelist 有find_package(Qt5Widgets REQUIRED),cmake 报错如下: CMake Error at CMakeL ...
- [Training Video - 6] [File Reading] Using log object in the Groovy class
Car c= new Car(log); c.print() class Car{ def log public Car(log){ this.log=log } public void print( ...
- Spring.net页面属性注入
.条件spring.web程序集 1.1 system.web配置 <httpHandlers> <add verb="*" path="*.aspx& ...
- easyui tabs update 强制刷新页面
var tab = artDialog.open.origin.$("#tabs").tabs("getTab", "公司评级"); var ...
- 设计模式(java)--Bridge模式之蜡笔与毛笔的故事
转自:吕震宇 http://www.cnblogs.com/zhenyulu/articles/67016.html#!comments 我想大家小时候都有用蜡笔画画的经历吧.红红绿绿的蜡笔一大盒,根 ...
- Spring获取HttpServletRequest对象
<!-- WEB.XML中配置相关的监听机制 --> <listener> <listener-class> org.springframework.web ...
- IE6,7,8在boostrap中兼容h5和css3
IE6.7.8版本(IE9以下版本)浏览器兼容html5新增的标签,引入下面代码文件即可: <script src="https://oss.maxcdn.com/libs/html5 ...
- 负载均衡-会话保持,session同步(转载)
一,什么负载均衡一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情况 ...
- redis 映射数据结构粗略
[字符串] sds结构,simple dynamic string.是redis底层字符串实现,结构为: typedef char *sds; struct sdshdr { // buf 已占用长度 ...
- SQL命令行修改数据库
增加列: alter table tableName add columnName varchar(30) 修改列类型:alter table tableName alter column colum ...