检查二叉树是否为求和树的迭代方法

时间:2018-10-02 08:21:09

标签: data-structures binary-tree

在Google上搜索了很多之后,我找不到用于检查Binary搜索树是否为求和树的迭代方法。

如果有人有答案,请将其张贴在这里。

1 个答案:

答案 0 :(得分:-2)

这是GeeksForGeeks link上的答案。

int isSumTree(struct node* node) 
{ 
    int ls, rs; 

    /* If node is NULL or it's a leaf node then 
       return true */
    if(node == NULL || 
            (node->left == NULL && node->right == NULL)) 
        return 1; 

   /* Get sum of nodes in left and right subtrees */
   ls = sum(node->left); 
   rs = sum(node->right); 

   /* if the node and both of its children satisfy the 
       property return 1 else 0*/
    if((node->data == ls + rs)&& 
            isSumTree(node->left) && 
            isSumTree(node->right)) 
        return 1; 

   return 0; 
}