【问题标题】:Check if a binary tree is full in Java?检查Java中的二叉树是否已满?
【发布时间】:2015-11-21 10:30:17
【问题描述】:

我正在尝试编写一个方法,如果二叉树已满(每个节点有 2 个子节点或没有子节点)将返回 true,否则返回 false。这在某些时候有效,但不是全部。关于我哪里出错的任何建议?

public static void testNum4()
    {
        System.out.println("How many nodes do you want in your tree?");
        int num=sc.nextInt();
        //TreeNode<Integer> root = TreeUtil.createBalancedNumberTree(num);  Use to test for a balanced tree
        TreeNode<Integer> root = TreeUtil.createIntegerTree(num);
        TreeUtil.displayTreeInWindow(root);
        System.out.println(isFull(root));
        TreeUtil.displayTreeInWindow (root);
    }

    public static boolean isFull(TreeNode<Integer>  root) {
    // pre: root of tree, 0 or more nodes
    // post: returns true if the input tree is a full tree; false otherwise

        if (root!=null) {
            if ((root.getLeft() != null && root.getRight() != null) || (root.getRight() == null && root.getLeft() == null))
            {
                return true;
            }
            else if (root.getLeft()!=null)
            {
                isFull(root.getLeft());
            }
            else if (root.getRight()!=null)
            {
                isFull(root.getRight());
            }
            else 
                return false;

        }
            return false;
    }

【问题讨论】:

  • 你的问题能说清楚吗?
  • 如果树是完整的,我将永远正确。但是,如果它不完整,有时我会得到 true(这是错误的),有时会得到 false。
  • 第一个if 条件也不正确。如果一个节点(根)有 2 个子节点,则返回 true 而不递归地检查它们

标签: java algorithm tree


【解决方案1】:

定义:如果每个节点要么是叶子要么恰好拥有两个子节点,则二叉树 T 是满的。

public static boolean isFull(TreeNode<Integer>  root)
// pre: root of tree, 0 or more nodes
// post: returns true if the input tree is a full tree; false otherwise
{

    if (root!=null)
    {
        if(root.getRight() == null && root.getLeft() == null)
        {
             return true;
        }
        if ((root.getRight() != null && root.getLeft() != null))
        {
            return isFull(root.getLeft())&&isFull(root.getLeft());
        }
    }
    return false;
}

【讨论】:

  • 那么叶子测试在哪里?即左右都为空?
  • @weston 你的答案是对的,但是如果 RIGHT 节点为空而 LEFT 节点不是(可能是深子树),它会运行得有点慢。
  • 这是一个很好的观点。最好将其作为对我的答案的评论,以便查看我的答案的人会发现这一点。
【解决方案2】:

尝试在每个语句中添加返回。

    else if (root.getLeft()!=null  && root.getRight()!=null)
    {
        return isFull(root.getLeft()) && isFull(root.getRight());
   }

此外,如果根节点为空,则您的树已满。所以最后的返回应该是return true;

【讨论】:

  • 那么,如果左边满了,就连右边都不看?
【解决方案3】:

问题在于else if 和缺少return 语句。也无需过多检查null,使用方法使其更具可读性。

public static boolean isFull(TreeNode<Integer> node) {

    if (node == null) return false;

    if (isLeaf(node)) return true;

    return isFull(node.getLeft()) && isFull(node.getRight());
}

public static boolean isLeaf(TreeNode<Integer> node) {
    return node.getRight() == null && node.getLeft() == null;
}

【讨论】:

    【解决方案4】:

    您没有完全遍历树。使用递归来命中所有节点。检查根节点。如果没有孩子,则返回 true。如果有孩子,请确保有两个,然后递归检查每个孩子。

    【讨论】:

      【解决方案5】:

      我认为if语句应该如下:

      if (root.getRight() == null && root.getLeft() == null)
      {
          // The node has no children (full)
          return true;
      }
      else if (root.getLeft() != null && root.getRight() != null)
      {
          // There are two children. Tree is only full if both sub trees are full
          return isFull(root.getLeft()) && isFull(root.getRight());
      }
      else 
      {
          // Only one child
          return false;
      }
      

      【讨论】:

        【解决方案6】:

        在这种情况下,上面的所有算法都返回 true(因为它们不应该): complete binary tree .所以,希望这会有所帮助:

        //-1 means "false"
        public boolean full() {
            int high = 0;
            return ( root != null && isFull(root, high) != -1 );
        }
        
        public boolean isLeaf() {
            return node.getRight() == null && node.getLeft() == null;
        }
        
        private int isFull(TreeNode<T> node, int high)
        {
            ++high;
            if (node.isLeaf())
                return high;
            else
            {
                int hLeft=0, hRight=0;
                if(node.getLeft() != null)
                    hLeft = isFull(node.getLeft(), high);
                if(node.getRight() != null)
                    hRight = isFull(node.getRight, high);
        
                if ( (hLeft == hRight) && (hLeft != -1) )
                    return ++high;
                return -1;
            }
        }
        

        【讨论】:

        • All the algoritms [sic] above return true in this case - this 是哪种情况?鉴于该问题解释了binary tree is full (each node has 2 child nodes or none)(值得称赞的勤奋),这试图回答一个不同的问题。
        • 点击“完整二叉树”链接,您可以查看是哪种情况。如果您测试这些算法,它们将返回 true,它们不应该这样做,因为不是完整的树而是完整的树。
        • 这个答案的代码检查一棵树是否完美,这是一个不同的概念。所有完美的树都是满的,但反之则不然。
        猜你喜欢
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        • 2019-04-09
        • 1970-01-01
        相关资源
        最近更新 更多