This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void levelorderTraversal(TreeNode root) { | |
if (root == null) { | |
return; | |
} | |
Queue<TreeNode> queue = new LinkedList<>(); | |
queue.add(root); | |
while (!queue.isEmpty()) { | |
TreeNode node = queue.remove(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void postorderTraversal(TreeNode root) { | |
if (root != null) { | |
postorderTraversal(root.left); | |
postorderTraversal(root.right); | |
System.out.print(root.data + " "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void preorderTraversal(TreeNode root) { | |
if (root != null) { | |
System.out.print(root.data + " "); | |
preorderTraversal(root.left); | |
preorderTraversal(root.right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void inorderTraversal(TreeNode root) { | |
if (root != null) { | |
inorderTraversal(root.left); | |
System.out.print(root.data + " "); | |
inorderTraversal(root.right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TreeNode { | |
int data; | |
TreeNode left; | |
TreeNode right; | |
public TreeNode(int data) { | |
this.data = data; | |
this.left = this.right = null; | |
} | |
} |