0

Pseudocode

  1. Created a class that will hold the node and its horizontal height

  2. Using BFS, so create a queue and inserted the first node having a horizontal height of 0

  3. Popped the element from the queue, if the horizontal height doesn't exist in the map then created an entry for it

  4. get the ArrayList of horizontal height and add the value of the node to it

  5. check for the left and right child, if they are not null then add them to the queue

    class Solution {
    
      class Node{
        TreeNode key;
        int h;
        Node(TreeNode key,int h){
            this.key=key;
            this.h=h;
          }
       }
    
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        if(root==null)
            return null;
        TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
        Queue<Node> q=new LinkedList<>();
        q.add(new Node(root,0));
    
        while(!q.isEmpty()){
            Node tmp=q.poll();
            if(!map.containsKey(tmp.h))
                map.put(tmp.h,new ArrayList<Integer>());
            map.get(tmp.h).add(tmp.key.val);           
            if(tmp.key.left!=null)
                q.add(new Node(tmp.key.left,tmp.h-1));
            if(tmp.key.right!=null)
                q.add(new Node(tmp.key.right,tmp.h+1));
        }
    
        List<List<Integer>> ans=new ArrayList<>();
        for(ArrayList<Integer> al:map.values()){
            ans.add(al);
        }
        return ans;
    }
    

    }

Problem Failing for input

Input: [0,2,1,3,null,null,null,4,5,null,7,6,null,10,8,11,9] tree

enter image description here

  • `if(tmp.key.left!=null) q.add(new Node(tmp.key.left,tmp.h-1));` shouldn't it be `tmp.h+1` ? – kasptom Aug 10 '20 at 23:37
  • No, it's like a number line. If a node is at n height, then left will be at n-1 and right at n+1 –  Aug 10 '20 at 23:41
  • Isnt that horizontal hight ? Just imagine vertical vs horizontal line. Vertical high is also high of the node in the tree - from bottom to up. While horizontal height is position of the node from left to the right, as in the case of your task. – Norbert Dopjera Aug 10 '20 at 23:48
  • Yes, it is Norbert. Thanks for clarifying, I have already updated the question –  Aug 10 '20 at 23:50

1 Answers1

0

First of all you are probably talking about horizontal height not vertical height as your output suggest. Output you got seems to be correct since when making BFS traversal you are first looking on the left element then the right from up to bottom independently on horizontal heights. Left node of the tree level will always be processed sooner (therefore also its child will be sooner added to the queue) so at level 3 (from up to bottom indexing from 0) node with value 7 will be sooner added to the queue for processing then node with value 6. Therefore output seems to be correct in my eyes, can you tell us why your are expecting different output ?

According to this sentence in your task link (https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/):
"If two nodes have the same position, then the value of the node that is reported first is the value that is smaller."

It seems that you need to sort your sublists inside resulting list. You can do that with following code:

sortedResult = resultList.stream()
    .map(list -> list.stream().sorted().collect(Collectors.toList()))
    .collect(Collectors.toList());
Norbert Dopjera
  • 741
  • 5
  • 18
  • Yes, that is what I am trying to do. But I tried submitting on leetcode but it is not accepting my code. This is the link: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/ –  Aug 10 '20 at 23:43
  • 1
    Shouldn't your expected output contain node with value 1 on right most position inside expected output list ? – Norbert Dopjera Aug 10 '20 at 23:54
  • Problem is that you are getting correct output for BFS method but tests verifying your algorithm expect same horizontal value nodes to be sorted i will made edit to my answer. – Norbert Dopjera Aug 11 '20 at 00:05