Balanced Binary Tree---in VS2010 it's ok,but in leetcode it's RE


  • 0
    A
    class Solution {
    public:
        bool isBalanced(TreeNode *root) {
            if(root==NULL)return false;
            stack<TreeNode*> s;
            s.push(root);
            TreeNode *visited=new TreeNode(0);
            while(!s.empty()){
                TreeNode* t=s.top();
                bool noSon=true;
                if(t->left)
                if(t->left->left!=visited){
                    s.push(t->left);
                    noSon=false;
                }
                if(t->right)
                if(t->right->left!=visited){
                    s.push(t->right);
                    noSon=false;
                }
                if(noSon){
                    s.pop();
                    int left;
                    int right;
                    left=0;
                    right=0;
                    if(t->left)left=t->left->val;
                    if(t->right)right=t->right->val;
                    //if(left==0&&right==0)t->val=1;
                    if(left-right<=1&&left-right>=-1)t->val=(left>right?left:right)+1;
                    else return false;
                    t->left=visited;
                }
            }
            return true;
        }
    };

Log in to reply
 

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