[LeetCode刷题] Binary Tree Inorder Traversal

今天自己写了一下inorder traversal,写了一个挺neat的solution,感觉不错

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> l = new ArrayList<Integer>();
        if(root == null) {
            return l;
        }
        l.addAll(inorderTraversal(root.left));
        l.add(root.val);
        l.addAll(inorderTraversal(root.right));
        return l;
    }
}
posted on 2015-03-29 13:12  Seth_L  阅读(76)  评论(0编辑  收藏  举报