我在Java中找到了这个面试问题,我认为输出是在3号右边的左边顺序,意思是:
2 1 2 0 1
我是对的吗?
thnx !!
IntNode类:
public class IntNode;
{
private int _value;
private IntNode _next;
public IntNode (int val, IntNode n)
{
_value = val;
_next = n;
}
public IntNode getNext () {return _next;}
public void setNext (IntNode node) {_next = node;}
public int getValue () {return _value;}
public void setValue (int v) {_value = v;}
}
IntList类:
public class IntList
{
private IntNode _head;
public IntList() {_head = null;}
public void addHead (int val)
{
_head = new IntNode (val, _head);
}
}
节点类:
public class Node
{
private int _number;
private Node _leftSon, _rightSon;
public Node (int number)
{
_number = number;
_leftSon = null;
_rightSon = null;
}
public int getNumber() {return _number;}
public Node getLeftSon() {return _leftSon;}
public Node getRightSon() {return _rightSon;}
}
BinaryTree类:
The BinaryTree Class:
public class BinaryTree
{
public static IntList what (Node root , int i)
{
IntList level = new IntList();
if (root != null)
what (level, i, root);
return level;
}
private static void what (IntList level, int i, Node t)
{
if (t != null)
{
if (i>0)
{
what (level, i-1, t.getRightSon());
what (level, i-1, t.getLeftSon());
}
else
level.addHead(t.getNumber());
}
}
}
问题是:
1)对于以下树(pic),将在屏幕上打印以下行:
IntList list = BinaryTree.what(root, 3);
Sys...out... (list);
2)当她收到二叉树的根时,“什么”的方法是什么?
答案 0 :(得分:4)
在不运行代码的情况下,它看起来就像它所做的那样
因此,看起来它从右到左打印出深度为3的节点的所有值:
9, 1, 7, 6, 8