Created
May 25, 2019 15:11
-
-
Save madhur/80aa7ba89829d5d7b1a30dabcbc18087 to your computer and use it in GitHub Desktop.
Level order traversal
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 ArrayList<Integer> levelorder(TreeNode root) { | |
ArrayList<Integer> levelOrderedList = new ArrayList<Integer>(); | |
Queue<TreeNode> q = new LinkedList<TreeNode>(); | |
TreeNode curr = null; | |
if(root == null) { | |
return null; | |
} | |
q.add(root); | |
while(!q.isEmpty()) { | |
curr = (TreeNode)q.remove(); | |
//process current node | |
levelOrderedList.add(curr.data); | |
//System.out.print(curr.data); | |
if(curr.left != null) { | |
q.add(curr.left); | |
} | |
if(curr.right != null) { | |
q.add(curr.right); | |
} | |
} | |
return levelOrderedList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment