Why my test is right but submission Result is Wrong Answer?


  • 0
    L

    public static List<List<Integer>> resultList = new ArrayList<List<Integer>>();

    public static List<Integer> rightSideView(TreeNode root) {
    	List<Integer> result = new ArrayList<Integer>();
    	
    	getResultListNode(root, 0);
    	for(List<Integer> l : resultList){
    		result.add(l.get(l.size()-1));
    		System.out.println(l.get(l.size()-1));
    	}
    	
    	return result;
        
    }
    
    public static void getResultListNode(TreeNode root, int level){
    	if(root == null){
    		return;
    	}
    	if(resultList.size() > level){
    		resultList.get(level).add(root.val);
    	}else{
    		List<Integer> valueList = new ArrayList<Integer>();
    		valueList.add(root.val);
    		resultList.add(valueList);
    	}
    	
    	getResultListNode(root.left, level + 1);
    	getResultListNode(root.right, level + 1);
    	
    }

Log in to reply
 

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