recursive solution with map in java

  • 0
    public List<Integer> rightSideView(TreeNode root) {
            Map<Integer, Integer> map = new HashMap<>();
            int depth = 0;
            populateMapWithRightSideView(root, map, depth);
            return  new ArrayList<>( map.values());
        private void populateMapWithRightSideView(TreeNode root, Map<Integer, Integer> map, int depth) {
            if(root == null)
                map.put(depth, root.val);
            populateMapWithRightSideView(root.right, map, depth+1);
            populateMapWithRightSideView(root.left, map, depth+1);

Log in to reply

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