想错了又
错误的:
class Solution {
public:
vector
vector
if(root==NULL)
return jj;
jj=find(root);
if(jj.size()==2)
{
jj.erase(jj.begin()+jj.size()-1);
return jj;
}
else
{
if(jj.at(jj.size()-1)>jj.at(jj.size()-3))
{
vector
pp.push_back(jj.at(jj.size()-2));
pp.push_back(jj.at(jj.size()-1));
pp.erase(pp.begin()+pp.size()-1);
return pp;
}
else if(jj.at(jj.size()-1)==jj.at(jj.size()-3))
{
jj.erase(jj.begin()+jj.size()-3);
jj.erase(jj.begin()+jj.size()-1);
return jj;
}
else{
jj.erase(jj.begin()+jj.size()-1);
jj.erase(jj.begin()+jj.size()-1);
jj.erase(jj.begin()+jj.size()-1);
return jj;
}
}
}
vector
vector
vector
if(root->left==NULL && root->right==NULL)
{
vector
kk.push_back(root->val);
kk.push_back(1);
return kk;
}
if(root->left!=NULL)
l=find(root->left);
if(root->right!=NULL)
r=find(root->right);
if(l.empty()==1)
l=r;
else if( r.empty()==0 && l.at(l.size()-1)!=r.at(r.size()-1) )
{
if(r.at(r.size()-2)==root->val && l.at(l.size()-2)==root->val)
{
l.at(l.size()-1)=l.at(l.size()-1)+r.at(r.size()-1);
}
else if(r.at(r.size()-2)==root->val) l=r;
else if(l.at(l.size()-2)==root->val) l=l;
else
{
l=( l.at(l.size()-1) > r.at(r.size()-1) ? l:r );
}
}
else if(r.empty()==0 && l.at(l.size()-1)==r.at(r.size()-1))
{
if( r.at(r.size()-2) ==root->val && l.at(l.size()-2)==root->val )
l.at(l.size()-1)=l.at(l.size()-1)+r.at(r.size()-1);
else if(r.at(r.size()-2)==root->val) l=r;
else if(l.at(l.size()-2)==root->val) l=l;
else
{
int p=l.at(l.size()-1);
l.erase(l.begin()+l.size()-1);
int i=0;
for(i=0;i<(r.size()-1);i++)
{l.push_back(r.at(i));}
l.push_back(p);
}
}
vector
if(root->val==jj.at(jj.size()-2))
{
jj.at(jj.size()-1)= jj.at(jj.size()-1)+1;
return jj;
}
else{
if(jj.size()==2)
{
jj.push_back(root->val);
jj.push_back(1);
return jj;
}
else
{
if(jj.at(jj.size()-1)>jj.at(jj.size()-3))
{
vector
pp.push_back(jj.at(jj.size()-2));
pp.push_back(jj.at(jj.size()-1));
pp.push_back(root->val);
pp.push_back(1);
return pp;
}
else if(jj.at(jj.size()-1)==jj.at(jj.size()-3))
{
jj.erase(jj.begin()+jj.size()-3);
jj.push_back(root->val);
jj.push_back(1);
return jj;
}
else{
jj.erase(jj.begin()+jj.size()-1);
jj.erase(jj.begin()+jj.size()-1);
jj.push_back(root->val);
jj.push_back(1);
return jj;
}
}} }};
正确的:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void inOrder(TreeNode* root, TreeNode*& pre, int& curTimes,
int& maxTimes, vector
if (!root) return;
inOrder(root->left, pre, curTimes, maxTimes, res);
if (pre)
curTimes = (root->val == pre->val) ? curTimes + 1 : 1;
if (curTimes == maxTimes)
res.push_back(root->val);
else if (curTimes > maxTimes){
res.clear();
res.push_back(root->val);
maxTimes = curTimes;
}
pre = root;
inOrder(root->right, pre, curTimes, maxTimes, res);
}
vector
vector
if (!root) return res;
TreeNode* pre = NULL;
int curTimes = 1, maxTimes = 0;
inOrder(root, pre, curTimes, maxTimes, res);
return res;
}
};