天天看點

求二叉樹非葉子節點數

已知二叉樹中的結點類型用BinTreeNode表示,被定義為:

struct BinTreeNode{

char data; BinTreeNode *lChild, *rChild;};

其中:data為結點值域;lChild和rChild分别為指向左、右子女結點的指針域。

根據下面函數聲明編寫出求一棵二叉樹非葉子節點數目的算法。

int sum(BinTreeNode *node) 

解:

int sum(BinTreeNode *node) 

  if (node==null  ¦ ¦ (node->  lChild==null && node->  rChild==null)) return 0; 

  else return sum(node->  lChild)+sum(node->  rChild)+1; 

}

繼續閱讀