67. Binary Tree Inorder Traversal
Description
Given a binary tree, return the inorder traversal of its nodes' values.
Example
Given binary tree {1 ,
1
\
2
/
3
return [1 ,3 ,2 ].
Solution
import java.util.*;
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: A Tree
* @return : Inorder in ArrayList which contains node values.
*/
List<Integer> list = new ArrayList<Integer>();
public List<Integer> inorderTraversal (TreeNode root) {
if (root!=null ){
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
return list;
}
}