Java 8 Simple Solution


  • 0
    S
    public class Solution {
        public List<Integer> largestValues(TreeNode root) {
          	List<Integer> ret = new ArrayList<Integer>();
    		if (root == null)
    			return ret;
    		List<TreeNode> level = new ArrayList<TreeNode>();
    		level.add(root);
    
    		while (!level.isEmpty()) {
    			ret.add(level.stream().max((n1, n2) -> Integer.compare(n1.val, n2.val)).get().val);
    			level = Stream.concat(level.stream().map(n -> n.left), level.stream().map(n -> n.right))
    					.filter(n -> n != null).collect(Collectors.toList());
    		}
    
    		return ret;
        }
    }
    

Log in to reply
 

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