Java 6 lines recursive sol

  • 0

    I know this sol is better solved by iterative sol, but i just want to post a classic sol using recursive, and I think it is more concise than the Leetcode sol.

    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List list = new LinkedList();
            if(root == null) return list;
            if(inorderTraversal(root.left).size() != 0) list.addAll(inorderTraversal(root.left));
            if(inorderTraversal(root.right).size() != 0) list.addAll(inorderTraversal(root.right));
            return list;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.