【问题标题】:Restoring a whole binary tree by it's postorder traversal results通过后序遍历结果恢复一棵二叉树
【发布时间】:2017-03-24 15:50:18
【问题描述】:

我能否仅从一个排序为后序遍历的数组中恢复整个二叉树 (count(vertices) = 2^n-1)?

我建议的算法很简单,就是做后序遍历:

  1. 向左走
  2. 向右走
  3. current_vertex = 数组[i++]; // 初始化 i = 0

应该可以完成这项工作,不是吗?

【问题讨论】:

    标签: binary-tree binary-search-tree postorder


    【解决方案1】:

    不,你不能因为binary tree 中的post order array 可以转换为多个二叉树,它们不相同,因此无法知道哪一棵是原始的。

    例子-

    Post order'd 数组 2 3 5 可以转换为许多树,例如-

    2        // 3 is right child of 2 and 5 of 5
      3
        5
    

      5     //2 is left child and 3 is right child of 5
    2   3
    

    【讨论】:

    • 第一棵树不可能,因为它是一棵完整的(完整的)二叉树,所以只有一个这样的树。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2013-05-10
    • 2022-06-27
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多