XwinterwinterwinterX 2016-04-09 14:15 采纳率: 50%
浏览 2195
已采纳

请问树的垂直结构是怎样的?

         1
        /    \
       2      3
      / \    / \
     4   5  6   7
             \   \
              8   9 


The output of print this tree vertically will be:
4
2
1 5 6
3 8
7
9 

看不懂这个例子

  • 写回答

3条回答 默认 最新

  • threenewbee 2016-04-09 14:34
    关注
     http://stackoverflow.com/questions/20521098/print-a-tree-vertically
    
    >To understand what's same vertical line, we need to define horizontal distances first. If two nodes have the same Horizontal Distance (HD), then they are on same vertical line. The idea of HD is simple. HD for root is 0, a right edge (edge connecting to right subtree) is considered as +1 horizontal distance and a left edge is considered as -1 horizontal distance. For example, in the below tree, HD for Node 4 is at -2, HD for Node 2 is -1, HD for 5 and 6 is 0 and HD for node 7 is +2.
    
    关键点是理解HD,垂直距离。
    
              1(0)
            /    \
           2 (-1)   3(1)
          / \    / \
         4(-2)5(0) 6(0)   7(2)
                 \   \
                  8(1)   9(3)
    
    然后排序输出
    -2 4
    -1 2
    0 1 5 6
    1 3 8
    2 7
    3 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配