Check if binary tree is sum tree

June 24, 2022 · 1 min read

https://practice.geeksforgeeks.org/problems/sum-tree/1

class Solution
{
    int sumUtil(Node *root) {
        if (root == nullptr) return 0;

        return sumUtil(root->left) + sumUtil(root->right) + root->data;
    }

    public:
    bool isSumTree(Node* root)
    {
        if (root == nullptr ||
            (root->left == nullptr && root->right == nullptr)) {
            return true;
        }

        if (root->data == sumUtil(root->left) + sumUtil(root->right)
            && isSumTree(root->left) && isSumTree(root->right)) {
            return true;
        }
    }
};
Find minimum swaps required to convert a binary tree into BST
Check if all leaf nodes are at same level