二叉树节点镜像——填空
Binary tree node mirror - fill in the blanks
我目前正在备考,看了往年的考题,有一道关于二叉树的题,你必须填空,这样函数才能成为二叉树的子节点。
我对节点不是很了解,按道理我猜了一个,但不知道对不对。
问题如下:
public void mirror() {
mirror(root);
}
private void mirror(Node node) {
if (node != null) {
// do the subtrees
mirror(__________);
mirror(__________);
// swap the left/right pointers
Node temp = __________;
______________________;
______________________;
}
}
这是我的答案:
public void mirror() {
mirror(root);
}
private void mirror(Node node) {
if (node != null) {
// do the subtrees
mirror(node.left);
mirror(node.right);
// swap the left/right pointers
Node temp = node.left;
node.left = node.right;
node.right = temp;
}
}
如果有人可以帮助我并告诉我我是否在正确的轨道上,如果没有,请告诉我它应该是什么样子! :)
周末愉快!
你的回答是正确的。
祝考试顺利
我目前正在备考,看了往年的考题,有一道关于二叉树的题,你必须填空,这样函数才能成为二叉树的子节点。
我对节点不是很了解,按道理我猜了一个,但不知道对不对。
问题如下:
public void mirror() {
mirror(root);
}
private void mirror(Node node) {
if (node != null) {
// do the subtrees
mirror(__________);
mirror(__________);
// swap the left/right pointers
Node temp = __________;
______________________;
______________________;
}
}
这是我的答案:
public void mirror() {
mirror(root);
}
private void mirror(Node node) {
if (node != null) {
// do the subtrees
mirror(node.left);
mirror(node.right);
// swap the left/right pointers
Node temp = node.left;
node.left = node.right;
node.right = temp;
}
}
如果有人可以帮助我并告诉我我是否在正确的轨道上,如果没有,请告诉我它应该是什么样子! :)
周末愉快!
你的回答是正确的。
祝考试顺利