Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

binary tree level traversal

    public static void levelOrderTraversal(Node root)
    {
        // base case
        if (root == null) {
            return;
        }
 
        // create an empty queue and enqueue the root node
        Queue<Node> queue = new ArrayDeque<>();
        queue.add(root);
 
        // to store the current node
        Node curr;
 
        // loop till queue is empty
        while (!queue.isEmpty())
        {
            // process each node in the queue and enqueue their
            // non-empty left and right child
            curr = queue.poll();
 
            System.out.print(curr.key + " ");
 
            if (curr.left != null) {
                queue.add(curr.left);
            }
 
            if (curr.right != null) {
                queue.add(curr.right);
            }
        }
    }
Comment

level order traversal of binary tree c++

/*
node class contains:
node* left;
node* right;
int value;
*/
void levelOrder(node* root){
  node* ptr=nullptr;
  queue<node*> q;
  q.push(root);
  while(!q.empty()){
    ptr=q.front();
    cout<<ptr->value<<endl;
    if(ptr->left!=nullptr){
q.push(ptr->left);
  }
    if(ptr->right!=nullptr){
    q.push(ptr->right);
    }
    q.pop();
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: jmeter set var jsr 
Java :: java words from file 
Java :: flutter webview plugin background transparent 
Java :: set union java 
Java :: android select video samsung stackoverflow 
Java :: org.springframework.security.oauth2.jwt.JwtEncoder 
Java :: Sample HashMap 
Java :: JFrame change outline 
Java :: char value java 
Java :: enable cors on apache tomcat 
Java :: what is delegation in java 
Java :: 9999999999999 
Java :: javax.persistence.noresultexception: no entity found for query 
Java :: Retrieve User information in Spring Security 
Java :: find the third largest number in an array 
Java :: Java if...else 
Java :: Java TreeMap Example NavigableMap 
Java :: click on recyclerview item android animation 
Java :: java exception override message 
Java :: java string replace 
Java :: how to get filename without extension in java 
Java :: string vs new string 
Java :: Java List Remove Elements using remove() method 
Java :: encapsulation java 
Java :: convert from integer to character java 
Java :: path in spring form 
Java :: java to kotlin converter android studio 
Java :: run specific test case junit 
Java :: runtime exception in java 
Java :: java array 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =