Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int fun(Node *root,bool &ans){
- if(!root) return 0;
- if(!root->left && !root->right) return root->data;
- int n1=0,n2=0;
- n1=fun(root->left,ans);
- n2=fun(root->right,ans);
- if(n1+n2!=root->data) ans=false; //ans will keep track for any position if not works,then ans =false else true
- return n1+n2+root->data;
- }
- // Should return true if tree is Sum Tree, else false
- class Solution
- {
- public:
- bool isSumTree(Node* root)
- { if(!root || (!root->left && !root->right)) return true;
- bool ans=true;
- int temp=fun(root,ans);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement