Non-binary tree height

后端 未结 4 977
盖世英雄少女心
盖世英雄少女心 2021-01-14 11:21

Is there a way to find the height of a tree which is not necessarily binary? There are many algorithms for the height of a binary tree but none of them will work for a non-b

4条回答
  •  日久生厌
    2021-01-14 11:59

    Generally, you can extend most of algorithms for binary tree to non-binary.

    For example, for 2-tree:

    h(node) = max(h(left-child-of(node)) , h(right-child-of(node)))+1
    

    which can be extended to:

    h(node) = max(h(for-all-child-of(node)) + 1
    

提交回复
热议问题