Skip to content

Commit c9f7c8f

Browse files
authored
Merge pull request akgmage#1791 from akgmage/dev
add inorder traversal in go js and python
2 parents b6b94b9 + bd5793a commit c9f7c8f

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// TreeNode definition
6+
type TreeNode struct {
7+
Val int
8+
Left *TreeNode
9+
Right *TreeNode
10+
}
11+
12+
func inorderTraversal(root *TreeNode) []int {
13+
var result []int
14+
var stack []*TreeNode
15+
current := root
16+
17+
for current != nil || len(stack) > 0 {
18+
// Traverse all the way to the leftmost node, pushing each node onto the stack
19+
for current != nil {
20+
stack = append(stack, current)
21+
current = current.Left
22+
}
23+
24+
// Pop the top node from the stack (current leftmost node)
25+
current, stack = stack[len(stack)-1], stack[:len(stack)-1]
26+
27+
// Add the value of the current node to the result slice
28+
result = append(result, current.Val)
29+
30+
// Move to the right subtree of the current node
31+
current = current.Right
32+
}
33+
34+
return result
35+
}
36+
37+
func main() {
38+
// Create a sample binary tree
39+
root := &TreeNode{Val: 1, Right: &TreeNode{Val: 2, Left: &TreeNode{Val: 3}}}
40+
41+
// Perform in-order traversal
42+
result := inorderTraversal(root)
43+
44+
// Print the result
45+
fmt.Println(result) // Output: [1 3 2]
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class TreeNode {
2+
constructor(val) {
3+
this.val = val;
4+
this.left = this.right = null;
5+
}
6+
}
7+
8+
class Solution {
9+
inorderTraversal(root) {
10+
const result = [];
11+
const stack = [];
12+
let current = root;
13+
14+
while (current || stack.length > 0) {
15+
// Traverse all the way to the leftmost node, pushing each node onto the stack
16+
while (current) {
17+
stack.push(current);
18+
current = current.left;
19+
}
20+
21+
// Pop the top node from the stack (current leftmost node)
22+
current = stack.pop();
23+
24+
// Add the value of the current node to the result array
25+
result.push(current.val);
26+
27+
// Move to the right subtree of the current node
28+
current = current.right;
29+
}
30+
31+
return result;
32+
}
33+
}
34+
35+
// Example usage
36+
const root = new TreeNode(1);
37+
root.right = new TreeNode(2);
38+
root.right.left = new TreeNode(3);
39+
40+
const solution = new Solution();
41+
const result = solution.inorderTraversal(root);
42+
43+
console.log(result); // Output: [1, 3, 2]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class TreeNode:
2+
def __init__(self, val):
3+
self.val = val
4+
self.left = self.right = None
5+
6+
class Solution:
7+
def inorderTraversal(self, root):
8+
result = []
9+
stack = []
10+
current = root
11+
12+
while current or stack:
13+
# Traverse all the way to the leftmost node, pushing each node onto the stack
14+
while current:
15+
stack.append(current)
16+
current = current.left
17+
18+
# Pop the top node from the stack (current leftmost node)
19+
current = stack.pop()
20+
21+
# Add the value of the current node to the result list
22+
result.append(current.val)
23+
24+
# Move to the right subtree of the current node
25+
current = current.right
26+
27+
return result
28+
29+
# Example usage
30+
root = TreeNode(1)
31+
root.right = TreeNode(2)
32+
root.right.left = TreeNode(3)
33+
34+
solution = Solution()
35+
result = solution.inorderTraversal(root)
36+
37+
print(result) # Output: [1, 3, 2]

0 commit comments

Comments
 (0)