Can any one help me? just can not pass the last test case


  • 0
    Z

    It is really wired. I just can not pass the last test case. Can any one help me?

    public class Solution {
        public List<List<Integer>> verticalOrder(TreeNode root) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            if(root == null){
                return result;
            }
            Map<Integer, List<Integer>> map = new TreeMap<>();
            List<Integer> temp = new ArrayList<>();
            temp.add(root.val);
            map.put(0, temp);
            traverse(root, map, 0);
            for(Map.Entry<Integer, List<Integer>> entry : map.entrySet()){
                result.add(entry.getValue());
            }
            return result;
        }
        
        private void traverse(TreeNode root, Map<Integer, List<Integer>> map, int level){
            if(root == null){
                return;
            }
            if(root.left != null){
                if(!map.containsKey(level - 1)){
                    List<Integer> temp = new ArrayList<Integer>();
                    temp.add(root.left.val);
                    map.put(level - 1, temp);
                } else{
                    List<Integer> temp = map.get(level - 1);
                    temp.add(root.left.val);
                    map.put(level - 1, temp);
                }
            }
            if(root.right != null){
                if(!map.containsKey(level + 1)){
                    List<Integer> temp = new ArrayList<Integer>();
                    temp.add(root.right.val);
                    map.put(level + 1, temp);
                } else{
                    List<Integer> temp = map.get(level + 1);
                    temp.add(root.right.val);
                    map.put(level + 1, temp);
                }
            }
            traverse(root.left, map, level - 1);
            traverse(root.right, map, level + 1);
        }
    }
    

Log in to reply
 

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