Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 验证给定级别的所有节点在二叉树中是否具有不同的值_Java_Algorithm_Recursion_Binary Tree - Fatal编程技术网

Java 验证给定级别的所有节点在二叉树中是否具有不同的值

Java 验证给定级别的所有节点在二叉树中是否具有不同的值,java,algorithm,recursion,binary-tree,Java,Algorithm,Recursion,Binary Tree,我的问题是,这个问题能在不使用任何数据结构(堆栈、列表等)的情况下解决吗?或者它需要一个数据结构?(如果可能的话,我也希望看到这两种情况下的解决办法) 问题是: 具有表示包含整数值的二叉树的BinaryTree类。假设已经实现了以下方法: public BinaryTree right(); // returns right children public BinaryTree left(); // returns left children public int val(); //

我的问题是,这个问题能在不使用任何数据结构(堆栈、列表等)的情况下解决吗?或者它需要一个数据结构?(如果可能的话,我也希望看到这两种情况下的解决办法)


问题是:

具有表示包含整数值的二叉树的BinaryTree类。假设已经实现了以下方法:

public BinaryTree right();  // returns right children
public BinaryTree left();   // returns left children
public int val();   // returns value of root node.
执行以下递归方法:

public static boolean allDifferentAtLevel(BinaryTree a, int lev){...}
仅当at级别lev的所有节点值都具有不同的值时,才会接收整数的二叉树并返回true


提前感谢您的时间。

我们可以使用
HashSet
跟踪
lev
th级别的数据

public static boolean allDifferentAtLevel(BinaryTree a, int lev){
    return checker(new HashSet<Integer>(),a,0,lev); //EmptySet, Root, CurrentLevel, Level
}

public static boolean checker(HashSet<Integer> set,BinaryTree a, int currentLevel, int lev) {
    if(a==null) //If current node of tree is null, return true.
        return true;

    if(currentLevel==lev) //If currentLevel is the required level, We don't need to move further.
                          //We can just validate the data at currentLevel and return the appropriate value.
    {
        int value=a.val();
        if(set.contains(value)) //If set contains the value, Duplication found.
            return false;

        set.add(value);
        return true;
    }

    //Check for both of the children.
    return checker(set,a.left(),currentLevel+1,lev) && checker(set,a.right(),currentLevel+1,lev);
}
public静态布尔AllDifferentitatLevel(二进制树a,int-lev){
返回检查程序(新的HashSet(),a,0,lev);//清空设置,根,当前级别,级别
}
公共静态布尔检查器(哈希集、二进制树a、int currentLevel、int lev){
if(a==null)//如果树的当前节点为null,则返回true。
返回true;
if(currentLevel==lev)//如果currentLevel是必需的级别,那么我们不需要进一步移动。
//我们可以在currentLevel上验证数据并返回适当的值。
{
int值=a.val();
if(set.contains(value))//如果set包含该值,则会发现重复。
返回false;
增加(价值);
返回true;
}
//检查两个孩子。
返回检查程序(set,a.left(),currentLevel+1,lev)和检查程序(set,a.right(),currentLevel+1,lev);
}
我们可以使用
HashSet
跟踪
lev
th级别的数据

public static boolean allDifferentAtLevel(BinaryTree a, int lev){
    return checker(new HashSet<Integer>(),a,0,lev); //EmptySet, Root, CurrentLevel, Level
}

public static boolean checker(HashSet<Integer> set,BinaryTree a, int currentLevel, int lev) {
    if(a==null) //If current node of tree is null, return true.
        return true;

    if(currentLevel==lev) //If currentLevel is the required level, We don't need to move further.
                          //We can just validate the data at currentLevel and return the appropriate value.
    {
        int value=a.val();
        if(set.contains(value)) //If set contains the value, Duplication found.
            return false;

        set.add(value);
        return true;
    }

    //Check for both of the children.
    return checker(set,a.left(),currentLevel+1,lev) && checker(set,a.right(),currentLevel+1,lev);
}
public静态布尔AllDifferentitatLevel(二进制树a,int-lev){
返回检查程序(新的HashSet(),a,0,lev);//清空设置,根,当前级别,级别
}
公共静态布尔检查器(哈希集、二进制树a、int currentLevel、int lev){
if(a==null)//如果树的当前节点为null,则返回true。
返回true;
if(currentLevel==lev)//如果currentLevel是必需的级别,那么我们不需要进一步移动。
//我们可以在currentLevel上验证数据并返回适当的值。
{
int值=a.val();
if(set.contains(value))//如果set包含该值,则会发现重复。
返回false;
增加(价值);
返回true;
}
//检查两个孩子。
返回检查程序(set,a.left(),currentLevel+1,lev)和检查程序(set,a.right(),currentLevel+1,lev);
}

是的,这可以通过递归解决,无需额外的数据结构

让我们尝试定义不同
lev
s的
alldifferentitatlevel(BinaryTree,int-lev)
方法应该是什么样子:

对于
lev=0
来说,结果只是
false
,因为级别
0
上的所有节点都具有相同的值。级别
0
上只有一个节点,因此所有节点都具有相同的值

对于
lev=1
检查
tree.right().val()==tree.left().val()
(添加
null
检查)非常简单

对于更高级别(
lev>1
),应该递归调用该方法。基本上,
alldifferentitatlevel(tree.left(),lev-1)
alldifferentitatlevel(tree.right(),lev-1)
将确保左子树和右子树满足条件。不幸的是,这是不够的,因为左子树和右子树之间可能有一些共同的值

但有可能解决这一问题,不仅是左、右检查,而且所有子树的组合都有两个更深的层次。比如:

BinaryTree ll = tree.left() == null ? null : tree.left().left();
BinaryTree lr = tree.left() == null ? null : tree.left().right();
BinaryTree rl = tree.right() == null ? null : tree.right().left();
BinaryTree rr = tree.right() == null ? null : tree.right().right();

BinaryTree ll_lr = tree.left();
BinaryTree ll_rl = new BinaryTree(0, ll, rl);
BinaryTree ll_rr = new BinaryTree(0, ll, rr);
BinaryTree lr_rl = new BinaryTree(0, lr, rl);
BinaryTree lr_rr = new BinaryTree(0, lr, rr);
BinaryTree rl_rr = tree.right();

return allDifferentAtLevel(ll_lr, lev - 1) &&
       allDifferentAtLevel(ll_rl, lev - 1) &&
       allDifferentAtLevel(ll_rr, lev - 1) &&
       allDifferentAtLevel(lr_rl, lev - 1) &&
       allDifferentAtLevel(lr_rr, lev - 1) &&
       allDifferentAtLevel(rl_rr, lev - 1);
有四个子树,更深两层(
ll
lr
rl
rr
)。但我们不能只检查这些子树,我们必须相互检查。这些子树可能有六对不同的子树。为了相互检查这些子树,我们可以为每个不同的对创建一个二叉树(
ll_lr
ll_rl
ll_rr
lr_rl
lr_rr
),然后递归检查每个二叉树。如果任何子树
ll
lr
rl
rr
lev-2
上具有相等的元素,则在
lev-1
上将有一对子树具有相等的元素

是的,这个问题可以通过递归解决,而不需要额外的数据结构。我不认为<代码> BinaryTree <代码>这里是一个附加的数据结构。


话虽如此,使用像集合这样的附加数据结构将使任务更容易。

是的,这可以通过递归解决,而无需附加数据结构

让我们尝试定义不同
lev
s的
alldifferentitatlevel(BinaryTree,int-lev)
方法应该是什么样子:

对于
lev=0
来说,结果只是
false
,因为级别
0
上的所有节点都具有相同的值。级别
0
上只有一个节点,因此所有节点都具有相同的值

对于
lev=1
检查
tree.right().val()==tree.left().val()
(添加
null
检查)非常简单

对于更高级别(
lev>1
),应该递归调用该方法。基本上,
alldifferentitatlevel(tree.left(),lev-1)
alldifferentitatlevel(tree.right(),lev-1)
将确保左子树和右子树满足条件。不幸的是,这是不够的,因为左子树和右子树之间可能有一些共同的值

但有可能解决这一问题,不仅是左、右检查,而且所有子树的组合都有两个更深的层次。比如:

BinaryTree ll = tree.left() == null ? null : tree.left().left();
BinaryTree lr = tree.left() == null ? null : tree.left().right();
BinaryTree rl = tree.right() == null ? null : tree.right().left();
BinaryTree rr = tree.right() == null ? null : tree.right().right();

BinaryTree ll_lr = tree.left();
BinaryTree ll_rl = new BinaryTree(0, ll, rl);
BinaryTree ll_rr = new BinaryTree(0, ll, rr);
BinaryTree lr_rl = new BinaryTree(0, lr, rl);
BinaryTree lr_rr = new BinaryTree(0, lr, rr);
BinaryTree rl_rr = tree.right();

return allDifferentAtLevel(ll_lr, lev - 1) &&
       allDifferentAtLevel(ll_rl, lev - 1) &&
       allDifferentAtLevel(ll_rr, lev - 1) &&
       allDifferentAtLevel(lr_rl, lev - 1) &&
       allDifferentAtLevel(lr_rr, lev - 1) &&
       allDifferentAtLevel(rl_rr, lev - 1);
有四个子树,更深两层(