作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我制作了自己的 Tree 类,并尝试检查两棵树是否相同。但这里的问题是我正在使用这个电话:
Tree myTree = new Tree();
Tree mySecondTree = new Tree();
myTree.isIdentical(myTree, mySecondTree);
这样传递有点奇怪,我想这样传递:
myTree.isIdentical(mySecondTree);
isIdentical 函数:
class Tree<T>{
T data;
Tree left;
Tree right;
Tree(T data){
this.data = data;
}
public boolean isIdentical(Tree t1, Tree t2){
if(t1 == t2)
return true;
if(t1==null || t2==null)
return false;
return (
(t1.data == t2.data) &&
(isIdentical(t1.left, t2.left)) &&
(isIdentical(t1.right, t2.right))
);
}
}
我试过使用 Stack,但我有点卡在这上面
最佳答案
既然要这样执行
myTree.isIdentical(mySecondTree);
你可以这样做
public boolean isIdentical(Tree t2){
Tree t1 = this;
return isIdentical(t1, t2);
}
private boolean isIdentical(Tree t1, Tree t2){
if(t1 == t2)
return true;
if(t1==null || t2==null)
return false;
return (
(t1.data == t2.data) &&
(isIdentical(t1.left, t2.left)) &&
(isIdentical(t1.right, t2.right))
);
}
关于java - 检查两棵树是否相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328083/
我想构建 2 个树结构。第一个树将包含节点,每个节点都有我的 Range 对象的列表: class Range { public DateTime Start { get; set; }
我是一名优秀的程序员,十分优秀!