遞迴,給你個思路吧
1、求葉子個數
//Node為二叉樹節點型別,呼叫時引數為樹的根num(root);
intnum(Node*node)
{
if(node==NULL)return0;
return1+num(node->left)+num(node->right);
}
2、高度
intheight(Node*node)
else
intx=height(node->right);
inty=height(node->left);
if(x>y)returnx+1;
elsereturny+1;
3、複製
Node*copy(Node*node)
if(node==NULL)returnNULL;
Node*left=copy(node->left);
Node*right=copy(node->right);
Node*newnode=(Node*)malloc(sizeof(Node));
newnode->val=node->val;
newnode->left=left;
newnode->right=right;
returnnewnode;
遞迴,給你個思路吧
1、求葉子個數
//Node為二叉樹節點型別,呼叫時引數為樹的根num(root);
intnum(Node*node)
{
if(node==NULL)return0;
return1+num(node->left)+num(node->right);
}
2、高度
intheight(Node*node)
{
if(node==NULL)return0;
else
{
intx=height(node->right);
inty=height(node->left);
if(x>y)returnx+1;
elsereturny+1;
}
}
3、複製
Node*copy(Node*node)
{
if(node==NULL)returnNULL;
Node*left=copy(node->left);
Node*right=copy(node->right);
Node*newnode=(Node*)malloc(sizeof(Node));
newnode->val=node->val;
newnode->left=left;
newnode->right=right;
returnnewnode;
}