Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java binart tree print

 public void printTree(){
        int height = maxHeight(root);
        for(int i = 0;i < height;i++){
            printLevel(root, i);
        }

    }

    private void printLevel(Node node, int level){
        if(node == null){
            return;
        }

        if(level == 0){
            System.out.println(node.value+" ");
        }else{
            printLevel(node.left, level - 1);
            printLevel(node.right, level -1);
        }
    }


    private int maxHeight(Node root){
        if(root == null){
            return 0;
        }
        return 1+Math.max(maxHeight(root.left), maxHeight(root.right));
    }
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #java #binart #tree #print
ADD COMMENT
Topic
Name
6+8 =