[英]Print all paths in a BST in java
BST中的路徑是從根到葉節點的一個遍歷。 因此,如果我們有以下形式的二叉樹,
7
3 9
1 5 8 13
路徑是
7 3 1
7 3 5
7 9 8
7 9 13
這是我的代碼,無法正常工作。
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);
}
此代碼未正確打印所有路徑。 任何幫助表示贊賞。
//不需要迭代器,只需使用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);
}
}
一種基於預遍歷的自我記錄解決方案。 這應該與二叉樹一起使用(不需要BST):
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();
}
}
終於設法修復了我的代碼。 為了完整性起見。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.