C++ Solution O(h) Space O(n) runtime ; without using STL Containers


  • 0
    class Solution {
    public:
        bool b = false;
        bool checkEqualTree(TreeNode* root) {
            int sum = chsum(root);
            if((sum%2 != 0) || root == NULL || (root->left == NULL && root->right == NULL)) 
                return false;
            chsum(root,sum/2);
            return b;
        }
         int chsum(TreeNode* root,double chk=0.5){
            if(root == NULL || b) return 0;
            int sum = root->val+chsum(root->left, chk) + chsum(root->right, chk);
            if(sum == chk) b = true; 
            return sum;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.