My Clean Recursive Solution --- Java


  • 0
    B
    public class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            if(root == null){
                return res;
            }
            helper(res, root);
            return res;
        }
        
        private void helper(List<Integer> res, TreeNode node){
            if(node.left != null){
                helper(res, node.left);
            }
            res.add(node.val);
            if(node.right != null){
                helper(res, node.right);
            }
        }
    }

Log in to reply
 

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