【问题标题】:Print all paths in a BST in java在java中打印BST中的所有路径
【发布时间】:2019-03-22 20:16:40
【问题描述】:

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);

    }

此代码未正确打印所有路径。任何帮助表示赞赏。

【问题讨论】:

  • 你能提供一些关于“路径”的信息吗?您是否正在寻找 in-order 遍历的所有排列?
  • 我相信 OP 意味着从所有叶子到根的路径。
  • 路径是指从根到任何叶节点的路径。

标签: java binary-search-tree


【解决方案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】:

    // 我在这里使用递归,它也适用于不是 BST 的二叉树

    // 不需要迭代器,只需使用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);
    
        }
    }
    
    
    

    【讨论】:

      【解决方案3】:

      终于设法修复了我的代码。为了完整起见发布。

      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();
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-28
        • 1970-01-01
        • 2016-03-06
        • 1970-01-01
        • 2021-05-03
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多