We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents aa99a49 + 0d85d25 commit 1a10732Copy full SHA for 1a10732
kotlin/94-binary-tree-inorder-traversal.kt
@@ -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
24
25
26
27
+ fun inOrder(node: TreeNode?) {
28
+ node?: return
29
+ inOrder(node.left)
30
31
+ inOrder(node.right)
32
33
34
+ inOrder(root)
35
36
37
0 commit comments