Unable to understand the testcase for max depth of binary tree


  • 0
    J
        /**
         * Definition for a binary tree node.
         * public class TreeNode {
         *     int val;
         *     TreeNode left;
         *     TreeNode right;
         *     TreeNode(int x) { val = x; }
         * }
         */
        public class Solution {
            public int maxDepth(TreeNode root) {
        		if(root==null){
        			return 0;
        		}
        		int level=1;
        		HashMap<Integer, Integer>hm=new HashMap<>();
        		maxDepth(root,level,hm);
        		TreeSet<Integer>ts=new TreeSet<>();
        		ts.addAll(hm.values());
        		return ts.last();
        	}
        	private void maxDepth(TreeNode root,int level,HashMap<Integer, Integer> hm) {
        		if(root==null){
        			return;
        		}
        		hm.put(root.val, level);
        		maxDepth(root.left,level+1,hm);
        		maxDepth(root.right,level+1,hm);
        	}
        }
    
    /* This solution is failing for below test case :
    *   Input:	[-9,5,0,-2,-6,null,null,5,null,null,-3,6,-5,null,null,null,0]
    *  Output:	5
    *   Expected:	6
    *  As I am unable to understand the tree structure from above input so it's getting difficult for me to debug   my code. Kindly tell me the tree structure for above input so that I can understand where I am going  wrong.*/

Log in to reply
 

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