【问题标题】:How to find the leaf-only parent nodes in an n-ary tree如何在 n 叉树中找到只有叶的父节点
【发布时间】:2017-05-10 07:34:26
【问题描述】:

我正在尝试解决以下算法:

你有一棵 n 叉树。找到满足以下条件的所有节点 条件:

  • 该节点有子节点,但所有子节点都是叶子(它们没有子节点)。返回仅叶子的父节点列表和 它们在树中的深度。

所以如果我有下面的树,那么满足上述条件的唯一节点就是D,因为它有后代(E)但他们没有孩子。

  I am root!
     /\ \
    A B  F
      /\
     C  D
         \
         E

我正在尝试在 Java 中实现这一点,但伪代码也适用于我。 我在这里实现了树和节点结构:N-ary trees in Java

我只需要算法。

【问题讨论】:

    标签: algorithm tree


    【解决方案1】:
    1. 从根开始
    2. 当左子存在时:去左子
    3. 回到父亲身边,检查下一个儿子
    4. 如果没有其他儿子:将父亲插入列表
    5. 否则将父亲插入列表并转到第 2 步,但保留一个深度计数器,如果找到孙子:从列表中删除父亲
    6. 如果完成所有节点:返回列表

      根 / \ \ A B F / \ 光盘 \ E

    运行示例:

    • 去A
    • 返回根目录并将根目录插入列表
    • 去B
    • 转到 C(由于计数器而从潜在中删除根)
    • 返回 B 并将 B 添加到列表中
    • 去D
    • 转到 E(由于计数器,将 B 从电位中移除)
    • 返回 D 并插入列表
    • 回到B
    • 回到根目录
    • 转到 F(不要插入 root,因为 root 已经插入 [并删除]
    • 返回只有 D 的列表

    要完成这项工作,您应该为正在检查的节点运行一个计数器(以查看是否存在孙子节点),并且您应该有一种方法可以知道一个节点是否已从列表中删除,这样您就不会插入它再次(我没有明确写它,但我使用了 2 个列表 - 1 个用于潜力,1 个用于最终)

    【讨论】:

      【解决方案2】:

      好的,我明白了。这是我已经达到的解决方案。不过,我确信有更好的解决方案 - 欢迎您纠正我。

      // kick off the recursion
      public Map<Integer, Integer> findLeafOnlyParents(GenericTree<Integer> tree){
              Map<Integer, Integer> resultMap = new HashMap<>();
      
          // start search from the root
              traverseWithDepth(tree.getRoot(), resultMap, 0);
      
              return resultMap;
          } 
      
      
      private void traverseWithDepth(GenericTreeNode<Integer> node,  Map<Integer, Integer> traversalResult, int depth) {
      
        // check if the current note in the traversal is parent Of leaf-only nodes
        if (isParentOfLeafOnly(node)){
          traversalResult.put(node.data, depth);
        }
      
        // check the node's children
        for(GenericTreeNode<Integer> child : node.getChildren()) {
          traverseWithDepth(child, traversalResult, depth + 1);
        }
      
      }
      
      // the main logic is here
      private boolean isParentOfLeafOnly(GenericTreeNode<Integer> node){
        boolean isParentOfLeafOnly = false;
      
        // check if the node has children
        if(node.getChildren().size() > 0){
      
          // check the children of the node - they should not have children
          List<GenericTreeNode<Integer>> children = node.getChildren();
          boolean grandChildrenExist = false; 
      
          // for each child check if it has children of its own
          for(GenericTreeNode<Integer> child : children) {
            grandChildrenExist = child.getChildren().size() > 0;
      
            // once found note that the current node has grandchildren,
            // so we don't need to check the rest of the children of the node
            if (grandChildrenExist){
              break;
            }
           }
      
           // we need only the parents who don't have great children
          isParentOfLeafOnly = !grandChildrenExist;
        }
        return isParentOfLeafOnly;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        • 2015-08-24
        • 2015-11-04
        • 2015-01-05
        • 1970-01-01
        相关资源
        最近更新 更多