判斷給定的二叉樹是否為完全二叉樹

2021-05-21 23:55:00 字數 1049 閱讀 1285

完全二叉樹(complete binary tree):深度為k,有n個結點的二叉樹當且僅當其每乙個結點都與深度為k的滿二叉樹中編號從1到n的結點一一對應時,稱為完全二叉樹。

用了兩個輔助方法,遞迴實現,c# codes as below:

class

treenode

public

treenode rightson

public

string value

public treenode(string value = null, treenode leftson=null, treenode rightson=null)

//////intergrate method depthcount() and ifcompletedtree()

///

///

///

///

///

public

static

bool ifcompletedtree(treenode rootnode)

else }

//////

to get the depth of the tree

///

///

///

static

int depthcount(treenode rootnode)

else

if (rootnode.leftson == null && rootnode.rightson == null)

else }

//////

to judge if it is a completed tree

///

///

///

///

///

static

bool ifcompletedtree(treenode rootnode,int currentdepth,int depth)

else }

else }

}

判斷二叉樹是否為完全二叉樹

include include 節點 struct node 銷毀二叉樹 void destroy tree node root destroy tree root left destroy tree root right delete root 是否為完全二叉樹 bool is cbtree no...

判斷二叉樹是否為完全二叉樹

判斷二叉樹是否為完全二叉樹 完全二叉樹看起來就像是滿二叉樹右下角缺了一口。思路 需要引入乙個標誌位來區分兩個階段 1.先對該樹進行層序遍歷,會出現兩種階段 a 任何乙個節點都有兩顆子樹 當遇到乙個結點沒有子樹或者只有左子樹時,那麼就進入第二階段。當遇到乙個節點只有右子樹時,那麼這棵樹一定不是完全二叉...

二叉樹 判斷二叉樹是否為完全二叉樹

問題描述 判斷一棵二叉樹是否為完全二叉樹。知識點 完全二叉樹是指除二叉樹的最後一層外,其他各層的節點數達到最大個數,且最後一層的葉節點從左到右連續存在,只缺右側若干節點。演算法實現 class node is complete binary tree public static boolean is...