10 ms, simple Inorder Java (88.38%)


  • 0
    J
     public List<Integer> largestValues(TreeNode node) {
         List<Integer> list = new ArrayList<>();
        if (node == null) return list;
        levelwise(node, 0, list);
        return list;
    }
    
    private void levelwise(TreeNode node, int level, List<Integer> list) {
        if (node == null) return;
        if (list.size() < level + 1) list.add(Integer.MIN_VALUE);
    
        levelwise(node.left, level + 1, list);
        list.set(level, Math.max(node.val, list.get(level));
        levelwise(node.right, level + 1, list);
    }

Log in to reply
 

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