【发布时间】:2020-03-16 19:25:03
【问题描述】:
在练习二叉搜索树时寻求帮助。我无法弄清楚我的代码哪里出错了,因为它似乎遵循通用递归格式。可能与临时节点或返回语句有关?任何帮助将不胜感激。
public static Node < Integer > mirror(Node < Integer > root) {
if (root == null)
return null;
else {
mirror(root.left);
mirror(root.right);
Node temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
【问题讨论】:
-
假设您正在搜索需要合并一些
if statement的内容。然后继续遍历树,直到 if 计算结果为true,然后返回链上的值。您的树需要进行排序才能有效地工作。
标签: java recursion binary-tree