用java写
时间: 2024-01-04 08:02:05 浏览: 8
```
public class BTNode {
public int data;
public BTNode lchild;
public BTNode rchild;
public BTNode(int data) {
this.data = data;
this.lchild = null;
this.rchild = null;
}
}
public class Solution {
public void findParent(BTNode b, int x, BTNode[] p) {
if (b == null || (b.lchild == null && b.rchild == null)) {
p[0] = null;
return;
}
if ((b.lchild != null && b.lchild.data == x) || (b.rchild != null && b.rchild.data == x)) {
p[0] = b;
return;
}
findParent(b.lchild, x, p);
if (p[0] != null) {
return;
}
findParent(b.rchild, x, p);
}
}
```
注:Java中没有指针的概念,可以使用数组来模拟指针,上述代码中使用了BTNode类型的数组p来模拟BTNode类型的指针。