Skip to content

Commit 175c58c

Browse files
committed
Optional Binary Tree Morris Traversal Algorithms added.
1 parent ca81843 commit 175c58c

File tree

8 files changed

+378
-2
lines changed

8 files changed

+378
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-30
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
12+
/// Definition for a binary tree node.
13+
struct TreeNode {
14+
int val;
15+
TreeNode *left;
16+
TreeNode *right;
17+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
18+
};
19+
20+
21+
// InOrder Morris Traversal
22+
// Time Complexity: O(n), n is the node number in the tree
23+
// Space Complexity: O(1)
24+
class Solution {
25+
26+
public:
27+
vector<int> inorderTraversal(TreeNode* root) {
28+
29+
vector<int> res;
30+
if( root == NULL )
31+
return res;
32+
33+
TreeNode* cur = root;
34+
while(cur != NULL){
35+
36+
if(cur->left == NULL){
37+
res.push_back(cur->val);
38+
cur = cur->right;
39+
}
40+
else{
41+
TreeNode* prev = cur->left;
42+
while(prev->right != NULL && prev->right != cur)
43+
prev = prev->right;
44+
45+
if(prev->right == NULL){
46+
prev->right = cur;
47+
cur = cur->left;
48+
}
49+
else{
50+
prev->right = NULL;
51+
res.push_back(cur->val);
52+
cur = cur->right;
53+
}
54+
}
55+
}
56+
57+
return res;
58+
}
59+
};
60+
61+
int main() {
62+
63+
return 0;
64+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-31
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
/// Definition for a binary tree node.
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
20+
// Morris PostOrder Traversal
21+
//
22+
// Time Complexity: O(n)
23+
// Space Complexity: O(1)
24+
class Solution {
25+
26+
public:
27+
vector<int> postorderTraversal(TreeNode* root) {
28+
29+
vector<int> res;
30+
if(root == NULL)
31+
return res;
32+
33+
TreeNode* dummyRoot = new TreeNode(-1);
34+
dummyRoot->left = root;
35+
36+
TreeNode* cur = dummyRoot;
37+
while(cur != NULL){
38+
if(cur->left == NULL)
39+
cur = cur->right;
40+
else{
41+
TreeNode* prev = cur->left;
42+
while(prev->right != NULL && prev->right != cur)
43+
prev = prev->right;
44+
45+
if(prev->right == NULL){
46+
prev->right = cur;
47+
cur = cur->left;
48+
}
49+
else{
50+
prev->right = NULL;
51+
reverseTraversal(cur->left, res);
52+
cur = cur->right;
53+
}
54+
}
55+
}
56+
delete dummyRoot;
57+
58+
return res;
59+
}
60+
61+
private:
62+
void reverseTraversal(TreeNode* node, vector<int>& res){
63+
64+
int start = res.size();
65+
while(node != NULL){
66+
res.push_back(node->val);
67+
node = node->right;
68+
}
69+
reverse(res.begin() + start, res.end());
70+
}
71+
};
72+
73+
74+
void print_vec(const vector<int>& vec){
75+
for(int e: vec)
76+
cout << e << " ";
77+
cout << endl;
78+
}
79+
80+
int main() {
81+
82+
TreeNode* root = new TreeNode(1);
83+
root->right = new TreeNode(2);
84+
root->right->left = new TreeNode(3);
85+
print_vec(Solution().postorderTraversal(root));
86+
87+
return 0;
88+
}
89+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-29
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
#include <cassert>
9+
10+
using namespace std;
11+
12+
/// Definition for a binary tree node.
13+
struct TreeNode {
14+
int val;
15+
TreeNode *left;
16+
TreeNode *right;
17+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
18+
};
19+
20+
// PreOrder Morris Traversal
21+
// Time Complexity: O(n), n is the node number in the tree
22+
// Space Complexity: O(1)
23+
class Solution {
24+
25+
public:
26+
vector<int> preorderTraversal(TreeNode* root) {
27+
28+
vector<int> res;
29+
if(root == NULL)
30+
return res;
31+
32+
TreeNode* cur = root;
33+
while(cur != NULL){
34+
if(cur->left == NULL){
35+
res.push_back(cur->val);
36+
cur = cur->right;
37+
}
38+
else{
39+
TreeNode* prev = cur->left;
40+
while(prev->right != NULL && prev->right != cur)
41+
prev = prev->right;
42+
43+
if(prev->right == NULL){
44+
res.push_back(cur->val);
45+
prev->right = cur;
46+
cur = cur->left;
47+
}
48+
else{
49+
prev->right = NULL;
50+
cur = cur->right;
51+
}
52+
}
53+
}
54+
55+
return res;
56+
}
57+
};
58+
59+
int main() {
60+
61+
return 0;
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-30
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Stack;
8+
9+
// Inorder Morris Traversal
10+
// Time Complexity: O(n), n is the node number in the tree
11+
// Space Complexity: O(1)
12+
public class InorderSolution {
13+
14+
public List<Integer> inorderTraversal(TreeNode root) {
15+
16+
ArrayList<Integer> res = new ArrayList<Integer>();
17+
if(root == null)
18+
return res;
19+
20+
TreeNode cur = root;
21+
while(cur != null){
22+
23+
if(cur.left == null){
24+
res.add(cur.val);
25+
cur = cur.right;
26+
}
27+
else{
28+
TreeNode prev = cur.left;
29+
while(prev.right != null && prev.right != cur)
30+
prev = prev.right;
31+
32+
if(prev.right == null){
33+
prev.right = cur;
34+
cur = cur.left;
35+
}
36+
else{
37+
prev.right = null;
38+
res.add(cur.val);
39+
cur = cur.right;
40+
}
41+
}
42+
}
43+
return res;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-31
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Stack;
9+
10+
// Morris PostOrder Traversal
11+
//
12+
// Time Complexity: O(n)
13+
// Space Complexity: O(1)
14+
public class PostorderSolution {
15+
16+
public List<Integer> postorderTraversal(TreeNode root) {
17+
18+
ArrayList<Integer> res = new ArrayList<Integer>();
19+
if(root == null)
20+
return res;
21+
22+
TreeNode dummyRoot = new TreeNode(-1);
23+
dummyRoot.left = root;
24+
25+
TreeNode cur = dummyRoot;
26+
while(cur != null){
27+
if(cur.left == null)
28+
cur = cur.right;
29+
else{
30+
TreeNode pre = cur.left;
31+
while(pre.right != null && pre.right != cur)
32+
pre = pre.right;
33+
34+
if(pre.right == null){
35+
pre.right = cur;
36+
cur = cur.left;
37+
}
38+
else{
39+
pre.right = null;
40+
reverseTraversal(cur.left, res);
41+
cur = cur.right;
42+
}
43+
}
44+
}
45+
return res;
46+
}
47+
48+
private void reverseTraversal(TreeNode node, ArrayList<Integer> res){
49+
int start = res.size();
50+
while(node != null){
51+
res.add(node.val);
52+
node = node.right;
53+
}
54+
55+
int i = start, j = res.size() - 1;
56+
while(i < j){
57+
Integer t = res.get(i);
58+
res.set(i, res.get(j));
59+
res.set(j, t);
60+
61+
i ++;
62+
j --;
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-29
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
// PreOrder Morris Traversal
9+
// Time Complexity: O(n), n is the node number in the tree
10+
// Space Complexity: O(1)
11+
public class PreorderSolution {
12+
13+
public List<Integer> preorderTraversal(TreeNode root) {
14+
15+
ArrayList<Integer> res = new ArrayList<Integer>();
16+
if(root == null)
17+
return res;
18+
19+
TreeNode cur = root;
20+
while(cur != null){
21+
if(cur.left == null){
22+
res.add(cur.val);
23+
cur = cur.right;
24+
}
25+
else{
26+
TreeNode prev = cur.left;
27+
while(prev.right != null && prev.right != cur)
28+
prev = prev.right;
29+
30+
if(prev.right == null){
31+
res.add(cur.val);
32+
prev.right = cur;
33+
cur = cur.left;
34+
}
35+
else{
36+
prev.right = null;
37+
cur = cur.right;
38+
}
39+
}
40+
}
41+
42+
return res;
43+
}
44+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Definition for a binary tree node.
2+
public class TreeNode {
3+
int val;
4+
TreeNode left;
5+
TreeNode right;
6+
TreeNode(int x) { val = x; }
7+
}

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
| 6-7 优先队列相关的算法问题 Top K Frequent Elements | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/07-Top-K-Frequent-Elements/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/07-Top-K-Frequent-Elements/src/) |
9292
| 补充代码1:教科书经典前序非递归遍历 | [C++源码](06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/Optional-01-Classic-Non-Recursive-Preorder-Traversal/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/Optional-01-Classic-Non-Recursive-Preorder-Traversal/src/) |
9393
| 补充代码2:教科书经典中序非递归遍历 | [C++源码](06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/Optional-02-Classic-Non-Recursive-Inorder-Traversal/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/Optional-02-Classic-Non-Recursive-Inorder-Traversal/src/) |
94-
| 补充代码3:教科书经典后序非递归遍历 | [C++源码](06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/) |
95-
| 补充代码4:二叉树的Morris非递归遍历 | [C++源码] | [Java源码] |
94+
| 补充代码3:教科书经典后序非递归遍历 | [C++源码](06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/src/) |
95+
| 补充代码4:二叉树的Morris非递归遍历 | [C++源码](06-Stack-and-Queue/Course%20Code%20(C%2B%2B)/Optional-04-Binary-Tree-Morris-Traversal/) | [Java源码](06-Stack-and-Queue/Course%20Code%20(Java)/Optional-04-Binary-Tree-Morris-Traversal/src/) |
9696
| 补充代码5:双向广度优先搜索 Word Ladder | [C++源码] | [Java源码] |
9797
| **第七章:二叉树和递归** | [章节C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/07-Binary-Tree-and-Recursion/Course%20Code%20(C%2B%2B)/) | [章节Java源码](07-Binary-Tree-and-Recursion/Course%20Code%20(Java)/) |
9898
| 7-1 二叉树天然的递归结构 | [C++源码](https://github.com/liuyubobobo/Play-with-Algorithm-Interview/tree/master/07-Binary-Tree-and-Recursion/Course%20Code%20(C%2B%2B)/01-Maximum-Depth-of-Binary-Tree/) | [Java源码](07-Binary-Tree-and-Recursion/Course%20Code%20(Java)/01-Maximum-Depth-of-Binary-Tree/src) |

0 commit comments

Comments
 (0)