0
我正在尋找一些代碼,我已經用Java編寫了一個二叉樹類,並從中刪除了count參數參數,但保持整個事物的遞歸。二叉樹 - 遞歸計算一個級別上沒有計數參數的節點數量的方法(Java)
因此,考慮到這些變量的類:
public class BinaryTree<E> {
protected E data;
protected BinaryTree<E> left,right;
我怎麼能做到這一點的:
public int levelCount(int count, int level){
if (data == null) {return 0;}
if (count == level) {return 1;}
else {
return this.getRight().levelCount(count+1,level) + this.getLeft().levelCount(count+1,level);
}
}
這應該(和不)在任何給定的級別返回節點的數量二叉樹。
所以與樹 「thatTree」 看起來像:
2
/ \
6 3
/\ /\
4 5 7 10
thatTree.levelCount(0)返回1,thatTree.levelCount(1)返回圖2,thatTree.levelCount(2)返回4
任何特定語言? – 2014-10-29 00:38:51
請注意,這不是提供「一般批評」的網站。 – 2014-10-29 00:39:52
請舉例說明如何調用它以及它會返回什麼;目前還不清楚你在這裏計算什麼。 – 2014-10-29 00:42:21