Tree: Height of a Binary Tree

Sort by

recency

|

994 Discussions

|

  • + 0 comments

    For Java 15

    static class Node {
            Node left;
            Node right;
            int data;
            public Node(){}
            public Node(int data) {
                this.data = data;
            }
        }
        
        public static Node insert(Node root,int data) {
            Node n = new Node(data);
            Node head = root;
            if (root == null) {
                root = n;
            }
            while (head != null) {
                if (data < head.data && head.left == null) {
                    head.left = n;
                    break;
                } else if (data < head.data && head.left != null) {
                    head = head.left;
                } else if (data >= head.data && head.right != null) {
                    head = head.right;
                } else if (data >= head.data && head.right == null) {
                    head.right = n;
                    break;
                }
            }
            return root;
        }
    
        public static void main(String[] args) throws IOException {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
    			  // Not really used, this is the num of elements to be inserted
            int size = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<Integer> tree = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());
            
            Node root = insert(null, tree.get(0));
            
            for (int i = 1; i < tree.size(); i++) {
                root = insert(root, tree.get(i));
            }
            /*
            printPreOrder(root, bufferedWriter);
            bufferedWriter.newLine();
            */
    				
            // Here is where the logic must be placed...
            int height = getHeight(root);
            bufferedWriter.write(String.valueOf(height));
            
            bufferedReader.close();
            bufferedWriter.close();
        }
    
  • + 0 comments

    the input tree is broken, its not the same as shown in input ! what buggy code!! hackerrank FIX THIS !!!!!!!

  • + 0 comments

    Java 8

    public static int height(Node root) {
            if(root==null){
            return -1;
            }
    
            int left = height(root.left);
            int right = height(root.right);
    
            return Math.max(left, right)+1;
    }
    
  • + 0 comments
    def height(root, prev=0):
        return max(height(root.left), height(root.right))+1 if root is not None and (root.left is not None or root.right is not None) else prev
    `
    
  • + 0 comments

    node:internal/streams/writable:474 throw new ERR_INVALID_ARG_TYPE( ^ TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (6) at _write (node:internal/streams/writable:474:13)

    getting this though solution is fine :/