Last active
May 2, 2017 00:45
-
-
Save yeison/7ffc195b1e7bba76887117ee33366622 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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