【问题标题】:Print all root to leaf paths with there relative positions打印具有相对位置的所有根到叶路径
【发布时间】:2017-03-25 04:32:33
【问题描述】:

给定一棵二叉树,我们如何将根打印到叶子路径,但添加“_”表示相对位置?

例子:

Input : Root of below tree
         A 
       /   \  
      B      C 
     / \    / \
    D   E   F  G

Output : All root to leaf paths
_ _ A
_ B
D

_ A
B
_ E

 A
 _ C
 F

A
_ C
_ _ G

【问题讨论】:

    标签: algorithm recursion tree binary-tree


    【解决方案1】:

    您可以使用预订旅行来参观这棵树。用缩进记录路径。

    访问左孩子时减少缩进,访问右孩子时增加缩进。然后你就可以得到这样的路径,

    (0, A), (-1, B), (-2, D)
    (0, A), (-1, B), (0, E)
    ...
    

    在输出阶段,对路径进行归一化,找到路径节点的最小缩进,并将路径节点移到,

    (2, A), (1, B), (0, D)
    (1, A), (0, B), (1, E)
    ...
    

    然后相应地打印路径。

    这是python中的示例代码,

    def travel(node, indent, path):
        if not node:
            print_path(path)
            return
        path.append((indent, node))
        if node.left:
            travel(node.left, indent - 1, path)
        if node.right:
            travel(node.right, indent + 1, path)
        del path[-1]
    
    
    def print_path(path):
        min_indent = abs(min([x[0] for x in path]))
        for indent, node in path:
            p = []
            for x in xrange(min_indent + indent):
                p.append('_')
            p.append(node.val)
            print ' '.join(p)
    

    【讨论】:

      【解决方案2】:

      基于垂直顺序打印路径的想法。

      1) 我们对给定的二叉树进行前序遍历。在遍历树时,我们可以递归地计算水平距离或 HD。我们最初将水平距离作为 0 传递给根。对于左子树,我们将水平距离作为根的水平距离减 1。对于右子树,我们将水平距离作为根的水平距离加 1。对于每个 HD 值,我们在向量中维护一个节点列表(”这将存储当前节点水平距离和根的键值的信息“)。我们还维护节点的顺序(它们在从根到叶的路径中出现的顺序)。维护秩序。

      2) 当我们在遍历过程中到达叶节点时,我们用下划线“_”打印该路径

      a) First find the minimum Horizontal distance of the current path.
      b) After that we traverse current path
           First Print number of underscore “_” : abs (current_node_HD – minimum-HD)
           Print current node value.
      

      对所有根到叶路径执行此过程。

      【讨论】:

        【解决方案3】:

        我无法完全理解强劲对工作的反应。换了一些东西。

        class Node:
            def __init__(self, val):
                self.value = val
                self.right = None
                self.left = None
        
        
        def printTreeRelativePaths(root):
            indent = 0
            path = []
            preOrder(root, indent, path)
        
        def preOrder(node, indent, path):
        
            path.append((node, indent))
        
            if not node.left and not node.right:
                processPath(path)
        
            if node.left:
                preOrder(node.left, indent - 1, path)
            if node.right:
                preOrder(node.right, indent + 1, path)
            del path[-1]
        
        def processPath(path):
            minIndent = 0
            for element in path:
                if element[1] < minIndent:
                    minIndent = element[1]
            offset = abs(minIndent)
            for element in path:
                print ('_' * (offset + element[1])) + element[0].value
        
        
        
        root = Node('A')
        root.left = Node('B')
        root.right = Node('C')
        root.left.left = Node('D')
        root.left.right = Node('E')
        root.right.left = Node('F')
        root.right.right = Node('G')
        
        printTreeRelativePaths(root)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-29
          相关资源
          最近更新 更多