Leetcode(94) Binary Tree Inorder Traversal

Description:

Given a binary tree, return the inorder traversal of its nodes’ values. Example:

Input: [1,null,2,3]

1
2
3
4
5
1
\
2
/
3

Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

解法:

直接递归的方法解题,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> output = new ArrayList();
helper(root,output);
return output;
}
void helper(TreeNode root,List<Integer> output){
if(root == null){
return;
}
helper(root.left,output);
output.add(root.val);
helper(root.right,output);
}
}