【问题标题】:Binary Tree printing all paths二叉树打印所有路径
【发布时间】:2012-08-13 14:22:13
【问题描述】:

下面的设计可以进一步优化吗?我使用了哈希图和队列。所以空间复杂度为 O(n),运行时间为 O(n)

public class PrintAllRootToLeaves {

    public static void print(BinaryTreeNode root) {     

        Queue nodesQ = new Queue();
        HashMap hMap = new HashMap();
        BinaryTreeNode head = root;
        String tempVal ;        
        hMap.put(head,String.valueOf(head.getData()));
        while (head != null) {

            BinaryTreeNode left = head.getLeft();
            BinaryTreeNode right = head.getRight(); 

            if (left != null) {
                if ((tempVal = (String) hMap.get(head)) != null) {                  
                    hMap.put(left,tempVal + left.getData());
                }
                nodesQ.enqueue(left);
            }

            if (right != null) {
                if ((tempVal = (String) hMap.get(head)) != null) {  
                    hMap.put(right,tempVal + right.getData());

                }
                nodesQ.enqueue(right);
            }           
            if (right != null && left != null) {
                hMap.remove(head);
            }
            head = (BinaryTreeNode) nodesQ.dequeue();                       
        }       
        System.out.println("-----------Printing all routes ---------->" + hMap.values());               
    }
}

【问题讨论】:

  • 看来您需要遍历每个节点,所以我看不出复杂性如何小于 O(n)。您可以做的是使用 StringBuilder 而不是附加到字符串。
  • 为什么要在循环中从 hashmap 中删除节点,您是否只想打印最低的树节点?
  • 这不能在O(N*Log N)下完成,因为你有N*Log N要打印的项目。
  • @kditraglia,我正在从 hashmap 中删除节点以获取从根到叶的路径而不是中间路径?
  • @Peter,空间复杂度如何,有什么建议吗?

标签: java algorithm optimization tree binary-tree


【解决方案1】:
public class BinaryTree {


    private class Node {
        final int key;
        final int value;
        Node left;
        Node Right;

        public Node (Node node, int pKey, int qValue) {
            key = pKey;
            value = qValue;
            if (node != null && node.key < pKey) {
                left = node;
            }
            else if (node != null) {
                Right = node;
            }
        }
    }


    public void preOrderTraversal(Node pNode,String path){

        if (path == null) {
            path = "";
        }
        if (pNode != null) {
            path = path+" "+String.valueOf(pNode.key);
//If you remove the modulo check it will print all the paths.
            if (pNode.key%5 == 0) {
                System.out.println(path);
            }
            preOrderTraversal(pNode.left,path);

            preOrderTraversal(pNode.Right,path);

        }

    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        Node node1 = new BinaryTree().new Node(null, 5, 2);
        Node node2 = new BinaryTree().new Node(null, 10, 25);
        Node node3 = new BinaryTree().new Node(node1, 7, 50);
        node3.Right = node2;
        Node root = new BinaryTree().new Node(node3, 15, 6);
        Node node4 = new BinaryTree().new Node(null, 30, 8);
        Node node5 = new BinaryTree().new Node(node4, 20, 7);
        root.Right = node5;
//This will print paths only divisable by 5
        new BinaryTree().preOrderTraversal(root,null);
    }

}

【讨论】:

  • 这如何回答这个问题?
猜你喜欢
  • 2018-03-04
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多