Skip to content

Commit 1a10732

Browse files
authored
Merge pull request #1614 from a93a/94
Create 94-Binary-Tree-Inorder-Traversal.kt
2 parents aa99a49 + 0d85d25 commit 1a10732

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//iterative version
2+
class Solution {
3+
fun inorderTraversal(root: TreeNode?): List<Int> {
4+
val res = ArrayList<Int>()
5+
val stack = Stack<TreeNode>()
6+
7+
var node = root
8+
while(node != null || !stack.isEmpty()){
9+
while(node != null){
10+
stack.push(node)
11+
node = node.left
12+
}
13+
node = stack.pop()
14+
res.add(node.`val`)
15+
node = node.right
16+
}
17+
18+
return res
19+
}
20+
}
21+
22+
//recursion version
23+
class Solution {
24+
fun inorderTraversal(root: TreeNode?): List<Int> {
25+
val res = ArrayList<Int>()
26+
27+
fun inOrder(node: TreeNode?) {
28+
node?: return
29+
inOrder(node.left)
30+
res.add(node.`val`)
31+
inOrder(node.right)
32+
}
33+
34+
inOrder(root)
35+
return res
36+
}
37+
}

0 commit comments

Comments
 (0)