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);
}
此代码未正确打印所有路径。任何帮助表示赞赏。
答案 0 :(得分:1)
//不需要迭代器,只需使用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);
}
}
答案 1 :(得分:1)
基于前序遍历的稍微自我记录的解决方案。这应该适用于二叉树(不需要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();
}
}
答案 2 :(得分:0)
管理最终修复我的代码。发布为了完整起见。
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();
}