如何使用空格键(没有循环(for,while ...)和lambda)来改变边缘。
我有此代码:
public static String prettyPrint(Node tree) {
if(tree == null)
return "";
return "- " + tree.value + "\n" + prettyPrint(tree.left) + prettyPrint(tree.right);
}
返回是:
- f
- o
- C
- tasty
- F
- E
- e
但是它需要像这样出来:
- f
- o
- C
- tasty
- F
- E
- e
(树:f [o [C [tasty,null],F],E [null,e]])]
我不太擅长递归,你们能帮我吗?
添加了一个计数器来跟踪树的深度。
由于无法使用静态int值作为计数器,所以参数发生了变化。
[hypen(-)之前有14个空格
在Java中无法将字符串相乘(但是在python中是可能的,这就是为什么必须对其进行子字符串处理。
public static String prettyPrint(Node tree, int i) {
if (tree == null)
return "";
return " - ".substring(14 - i) + tree.value + "\n" + prettyPrint(tree.left, i + 1) + prettyPrint(tree.right, i + 1);
}