best companies to workk in
Java
// Java program for recursive level order traversal in spiral form
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class Node {
int data;
Node left, right;
public Node(int d)
{
data = d;
left = right = null;
}
}
class BinaryTree {
Node root;
// Function to print the spiral traversal of tree
void printSpiral(Node node)
{
int h = height(node);
int i;
/* ltr -> left to right. If this variable is set then the
given label is traversed from left to right */
boolean ltr = false;
for (i = 1; i <= h; i++) {
printGivenLevel(node, i, ltr);
/*Revert ltr to traverse next level in opposite order*/
ltr = !ltr;
}
}
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(Node node)
{
if (node == null)
return 0;
else {
/* compute the height of each subtree */
int lheight = height(node.left);
int rheight = height(node.right);
/* use the larger one */
if (lheight > rheight)
return (lheight + 1);
else
return (rheight + 1);
}
}
/* Print nodes at a given level */
void printGivenLevel(Node node, int level, boolean ltr)
{
if (node == null)
return;
if (level == 1)
System.out.print(node.data + " ");
else if (level > 1) {
if (ltr != false) {
printGivenLevel(node.left, level - 1, ltr);
printGivenLevel(node.right, level - 1, ltr);
}
else {
printGivenLevel(node.right, level - 1, ltr);
printGivenLevel(node.left, level - 1, ltr);
}
}
}
/* Driver program to test the above functions */
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(7);
tree.root.left.right = new Node(6);
tree.root.right.left = new Node(5);
tree.root.right.right = new Node(4);
System.out.println("Spiral order traversal of Binary Tree is ");
tree.printSpiral(tree.root);
}
}
// This code has been contributed by Mayank Jaiswal(mayank_24)
Also in Java:
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- java logical operator
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- java 8 add whole array to jsonarray
- Category
- Java
- Title
- Java array hashcode
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- how to return the first character in an array from a method java
- Category
- Java
- Title
- java calendar add minutes
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- java random number generator in range
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- java observable
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- processing pi
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- java file download
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- int to binary string java
- Category
- Java
- Title
- reverse number in java
- Category
- Java
- Title
- java valeur absolue
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- how to add to a file in java
- Category
- Java
- Title
- how to find length of array in java
- Category
- Java
- Title
- parallel sorting in java 8
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- how-to-use-volley-string-request-in-android
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- how to multiply a number by itself using for loop in java
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- java remove first character from string
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- recursive function for fibonacci series in java javascript
- Category
- Java
- Title
- logging in java
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- append to arraylist by index java
- Category
- Java
- Title
- how to fill a 2d array in java
- Category
- Java
- Title
- import scanner
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- how to check if a string contains only alphabets and space in java
- Category
- Java
- Title
- récupérer les données d un liste java
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- how to create a thread local variable in java
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- transformez integer en string
- Category
- Java
- Title
- how to remove all components from layeredPane java
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- java display message
- Category
- Java
- Title
- round no in java
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- remove extra blank spaces from string in java
- Category
- Java
- Title
- double to string java
- Category
- Java
- Title
- read lines of file randomly java
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- convert every character in string to arraylist jva
- Category
- Java
- Title
- add one character to string java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- java create arraly list
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- how to use beacon power in minecraft in java edition
- Category
- Java
- Title
- java 8 function supplier consumer
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- enum in java
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- java string to int
- Category
- Java
- Title
- java thread
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- java function for power
- Category
- Java
- Title
- java map foreach
- Category
- Java