Skip to content

Instantly share code, notes, and snippets.

@yeison
Last active May 2, 2017 00:45
Show Gist options
  • Save yeison/7ffc195b1e7bba76887117ee33366622 to your computer and use it in GitHub Desktop.
Save yeison/7ffc195b1e7bba76887117ee33366622 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ConvertTree {
public static void main(String[] args){
Node root = new Node();
List<List<Node>> lists = convert(root);
}
static List<List<Node>> convert(Node root){
if(root == null) return null;
List<List<Node>> result = new ArrayList<>();
LinkedList<Node> current = new LinkedList<>();
current.add(root);
while(current.size() > 0){
result.add(current);
LinkedList<Node> parents = current;
current = new LinkedList<>();
for(Node parent : parents){
if(parent.left != null){
current.add(parent.left);
}
if(parent.right != null){
current.add(parent.right);
}
}
}
return result;
}
}
class Node {
Node left;
Node right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment