A path in a BST is one traversal from root to a leaf node. Thus if we have a binary tree of the form,
7
3 9
1 5 8 13
The paths would be,
7 3 1
7 3 5
7 9 8
7 9 13
This is my code, which isn't working correctly.
public void printPath(Node root){
Deque<Node> stack = new ArrayDeque<>();
printPath(root, stack);
}
public void printPath(Node root, Deque<Node> stack){
if(root == null){
Iterator itr = stack.descendingIterator();
while(itr.hasNext()){
Node p = (Node) itr.next();
System.out.print(p.data + " ");
}
stack.poll();
System.out.println();
return;
}
stack.offerFirst(root);
printPath(root.left, stack);
printPath(root.right, stack);
}
This code is not printing all the paths right. Any help appreciated.
// No need for iterators, just use the Java ArrayList
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
/*
30
20 50
15 25 40 60
70
80
// This would print
30 20 15
30 20 25
30 50 40
30 50 60 70 80
*/
public class AllPathsToLeafArrayList {
private static void findPaths(TreeNode root) {
ArrayList list = new ArrayList();
findPaths(root, list);
}
private static void findPaths(TreeNode root, List list) {
if (root == null)
return;
list.add(root.data);
if (root.left == null && root.right == null) {
printPaths(list);
} else {
findPaths(root.left, list);
findPaths(root.right, list);
}
list.remove(list.size() - 1);
}
private static void printPaths(List list) {
for (Integer l : list) {
System.out.print(l + " ");
}
System.out.println();
}
public static void main(String[] args) {
TreeNode root = new TreeNode(30);
root.left = new TreeNode(10);
root.right = new TreeNode(50);
root.left.left = new TreeNode(15);
root.left.right = new TreeNode(25);
root.right.left = new TreeNode(40);
root.right.right = new TreeNode(60);
root.right.right.right = new TreeNode(70);
root.right.right.right.right = new TreeNode(80);
findPaths(root);
}
}
A slightly more self-documenting solution based on preorder traversal. This should work with a binary tree (no BST is needed):
public class BTPaths {
private static final class BST<T> {
final T key;
BST<T> left;
BST<T> right;
BST(T key) {
this.key = key;
}
}
public static void main(String[] args) {
BST<Integer> t = new BST<>(100);
t.left = new BST<>(50);
t.right = new BST<>(150);
t.left.right = new BST<>(75);
t.left.right.left = new BST<>(63);
t.right.left = new BST<>(125);
t.right.right = new BST<>(200);
preOrderPrintPaths(t, new ArrayDeque<>(10));
}
static <T> void preOrderPrintPaths(BST<T> node, Deque<BST<T>> q) {
if (node == null)
return;
if (node.left != null) {
q.addLast(node);
preOrderPrintPaths(node.left, q);
}
if (node.right != null) {
q.addLast(node);
preOrderPrintPaths(node.right, q);
}
if (node.left == null && node.right == null) {
System.out.println(q.stream().map(n -> n.key.toString()).collect(Collectors.joining
("->")) + "->" + node.key );
}
if (!q.isEmpty())
q.removeLast();
}
}
Managed to fix my code finally. Posting for the sake of completeness.
public void printPath(Node root){
Deque<Node> stack = new ArrayDeque<>();
printPath(root, stack);
}
public void printPath(Node root, Deque<Node> stack){
if(root == null) return;
stack.offerFirst(root);
printPath(root.left, stack);
printPath(root.right, stack);
if(root.left == null && root.right == null){
Iterator itr = stack.descendingIterator();
while(itr.hasNext()){
Node p = (Node) itr.next();
System.out.print(p.data + " ");
}
System.out.println();
}
stack.poll();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.