Skip to content

Commit 5787d76

Browse files
committed
Add binary tree preorder + reorder list.
1 parent d0470c9 commit 5787d76

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

BinaryTreePreorderTraversal.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Author: Annie Kim, [email protected]
3+
Date: Nov 11, 2013
4+
Problem: Binary Tree Preorder Traversal
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/
7+
Notes:
8+
Given a binary tree, return the preorder traversal of its nodes' values.
9+
For example:
10+
Given binary tree {1,#,2,3},
11+
1
12+
\
13+
2
14+
/
15+
3
16+
return [1,2,3].
17+
Note: Recursive solution is trivial, could you do it iteratively?
18+
19+
Solution: 1. Iterative way (stack). Time: O(n), Space: O(n).
20+
2. Recursive solution. Time: O(n), Space: O(n).
21+
3. Threaded tree (Morris). Time: O(n), Space: O(1).
22+
*/
23+
/**
24+
* Definition for binary tree
25+
* struct TreeNode {
26+
* int val;
27+
* TreeNode *left;
28+
* TreeNode *right;
29+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
30+
* };
31+
*/
32+
class Solution {
33+
public:
34+
vector<int> preorderTraversal(TreeNode *root) {
35+
return preorderTraversal_3(root);
36+
}
37+
38+
vector<int> preorderTraversal_1(TreeNode *root) {
39+
vector<int> res;
40+
stack<TreeNode *> stk;
41+
TreeNode *cur = root;
42+
while (cur || !stk.empty())
43+
{
44+
if (cur)
45+
{
46+
res.push_back(cur->val);
47+
stk.push(cur);
48+
cur = cur->left;
49+
}
50+
else if (!stk.empty())
51+
{
52+
cur = stk.top()->right;
53+
stk.pop();
54+
}
55+
}
56+
return res;
57+
}
58+
59+
vector<int> preorderTraversal_2(TreeNode *root) {
60+
vector<int> res;
61+
preorderTraversalRe(root, res);
62+
return res;
63+
}
64+
65+
void preorderTraversalRe(TreeNode *node, vector<int> &res) {
66+
if (!node) return;
67+
res.push_back(node->val);
68+
preorderTraversalRe(node->left, res);
69+
preorderTraversalRe(node->right, res);
70+
}
71+
72+
vector<int> preorderTraversal_3(TreeNode *root) {
73+
vector<int> res;
74+
TreeNode *cur = root;
75+
while (cur)
76+
{
77+
if (cur->left)
78+
{
79+
TreeNode *prev = cur->left;
80+
while (prev->right && prev->right != cur)
81+
prev = prev->right;
82+
83+
if (prev->right == cur)
84+
{
85+
cur = cur->right;
86+
prev->right = NULL;
87+
}
88+
else
89+
{
90+
res.push_back(cur->val);
91+
prev->right = cur;
92+
cur = cur->left;
93+
}
94+
}
95+
else
96+
{
97+
res.push_back(cur->val);
98+
cur = cur->right;
99+
}
100+
}
101+
return res;
102+
}
103+
};

ReorderList.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Author: Annie Kim, [email protected]
3+
Date: Nov 11, 2013
4+
Problem: Reorder List
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/reorder-list/
7+
Notes:
8+
Given a singly linked list L: L0->L1->...->Ln-1->Ln,
9+
reorder it to: L0->Ln->L1->Ln-1->L2->Ln-2->...
10+
You must do this in-place without altering the nodes' values.
11+
For example,
12+
Given {1,2,3,4}, reorder it to {1,4,2,3}.
13+
14+
Solution: Reverse the back half first, then reorder.
15+
*/
16+
17+
/**
18+
* Definition for singly-linked list.
19+
* struct ListNode {
20+
* int val;
21+
* ListNode *next;
22+
* ListNode(int x) : val(x), next(NULL) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
void reorderList(ListNode *head) {
28+
if (!head || !head->next) return;
29+
ListNode *slow = head, *fast = head->next->next;
30+
while (fast && fast->next) {
31+
fast = fast->next->next;
32+
slow = slow->next;
33+
}
34+
if (fast) slow = slow->next;
35+
ListNode *mid = slow, *cur = slow->next;
36+
while (cur->next) {
37+
ListNode *mov = cur->next;
38+
cur->next = mov->next;
39+
mov->next = mid->next;
40+
mid->next = mov;
41+
}
42+
cur = head;
43+
while (cur != mid && mid->next) {
44+
ListNode *mov = mid->next;
45+
mid->next = mov->next;
46+
mov->next = cur->next;
47+
cur->next = mov;
48+
cur = cur->next->next;
49+
}
50+
}
51+
};

0 commit comments

Comments
 (0)