Wrong answer in two testcase

the code is valid for only two case and rest is not please

@shudhanshu20092001_af6f20d24c617008 use this approach :

Recursive Approach

The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the first node in its level (Note that we traverse the left subtree before right subtree).

Java Code for Recursive Approach

 public  void LeftView() {

        int[] maxLevel = new int[1];
        maxLevel[0] = -1;
        dfs(root, 0, maxLevel);

    }

    public  void dfs(Node root, int level, int[] maxLevel) {

        if (root == null) {
            return;
        }


        if(level > maxLevel[0]){
            System.out.print(root.data+" ");
            maxLevel[0] = level;
        }

        dfs(root.left, level + 1, maxLevel);
        dfs(root.right, level + 1, maxLevel);
    }