任何人都可以解释我是如何解决一个表达式树,当我给x作为参数?解决二叉树
例如,我有方程((2 * x))+ 4,让我们说在参数中,x = 3. 这会给我们10,方法会返回这个。
我想这样做的方法是递归地做,但我不能这样做,因为参数必须是双x。
有什么想法?
下面是我到目前为止的代码。
public double evaluate(double x) throws ExpressionTreeNodeException {
ExpressionTreeNode n = new ExpressionTreeNode();
n.setValue(getValue());
n.setType(getType());
if (n.getRightChild() == null && n.getLeftChild() == null){
double RootLeaf = Double.parseDouble(n.getValue());
return RootLeaf;
} else {
double operand1 =
return()
}
}
为什么不能你使用这个递归? – Woot4Moo
你能不能解释一下怎么递归地做呢?如果我想这样做,我想我需要参数作为根,这样我才能继续通过树。 –