-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathMorrisTraversal.js
More file actions
53 lines (48 loc) · 1.43 KB
/
MorrisTraversal.js
File metadata and controls
53 lines (48 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Morris Traversal (Inorder)
*
* This algorithm performs an inorder traversal of a binary tree
* without using recursion or a stack. Instead, it creates temporary
* "threads" in the tree to traverse nodes in O(N) time and O(1) space.
*
* Time Complexity: O(N) (each edge visited at most twice)
* Space Complexity: O(1)
*
* @param {TreeNode} root - Root node of the binary tree
* @returns {number[]} - The inorder traversal as an array
*/
export class TreeNode {
constructor(val, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
export function morrisTraversal(root) {
const result = []
let current = root
while (current !== null) {
if (current.left === null) {
// Case 1: No left child → visit current node
result.push(current.val)
current = current.right
} else {
// Case 2: Find inorder predecessor
let predecessor = current.left
while (predecessor.right !== null && predecessor.right !== current) {
predecessor = predecessor.right
}
if (predecessor.right === null) {
// Create a temporary thread from predecessor to current
predecessor.right = current
current = current.left
} else {
// Thread already exists → remove it and visit current
predecessor.right = null
result.push(current.val)
current = current.right
}
}
}
return result
}