Skip to content

Commit 3fec98b

Browse files
committed
add solution of problem 94: binary tree inorder traversal
1 parent 06b5b11 commit 3fec98b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Given a binary tree, return the inorder traversal of its nodes' values.
2+
3+
For example:
4+
Given binary tree `[1,null,2,3]`,
5+
```
6+
7+
1
8+
\
9+
2
10+
/
11+
3
12+
13+
```
14+
15+
return `[1,3,2]`.
16+
17+
**Note:** Recursive solution is trivial, could you do it iteratively?
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public List<Integer> inorderTraversal(TreeNode root) {
12+
List<Integer> result = new ArrayList<Integer>();
13+
14+
inorder(root, result);
15+
return result;
16+
}
17+
18+
private void inorder(TreeNode node, List<Integer> result) {
19+
if (node == null)
20+
return;
21+
inorder(node.left, result);
22+
result.add(node.val);
23+
inorder(node.right, result);
24+
}
25+
}

0 commit comments

Comments
 (0)