代码如下:
/**
* 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:
bool isSameTree(TreeNode* p, TreeNode* q) {
stack<shared_ptr<TreeNode>> s1, s2;
if (p) {
s1.push(shared_ptr<TreeNode>(p));
}
if (q) {
s1.push(make_shared<TreeNode>(q));
}
while (!s1.empty() && !s2.empty()) {
shared_ptr<TreeNode> pT = s1.top(); s1.pop();
shared_ptr<TreeNode> qT = s2.top(); s2.pop();
if (pT->val != qT->val) {
return false;
}
if (pT->left) {
s1.push(make_shared<TreeNode>(pT->left));
}
if (qT->left) {
s2.push(make_shared<TreeNode>(qT->left));
}
if (s1.size() != s2.size()) {
return false;
}
if (pT->right) {
s1.push(make_shared<TreeNode>(pT->right));
}
if (qT->right) {
s2.push(make_shared<TreeNode>(qT->right));
}
if (s1.size() != s2.size()) {
return false;
}
}
return false;
}
};
代码错误提示如下:
In file included from /usr/local/include/c++/6.3.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /usr/local/include/c++/6.3.0/bits/allocator.h:46,
from /usr/local/include/c++/6.3.0/string:41,
from /usr/local/include/c++/6.3.0/bits/locale_classes.h:40,
from /usr/local/include/c++/6.3.0/bits/ios_base.h:41,
from /usr/local/include/c++/6.3.0/ios:42,
from /usr/local/include/c++/6.3.0/ostream:38,
from /usr/local/include/c++/6.3.0/iostream:39,
from headers.h:1:
/usr/local/include/c++/6.3.0/ext/new_allocator.h: In instantiation of 'void gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = TreeNode; _Args = {TreeNode*&}; _Tp = TreeNode]':
/usr/local/include/c++/6.3.0/bits/alloc_traits.h:455:4: required from 'static void std::allocator_traitsstd::allocator<_CharT >::construct(std::allocator_traitsstd::allocator<_CharT >::allocator_type&, _Up*, _Args&& ...) [with _Up = TreeNode; _Args = {TreeNode*&}; _Tp = TreeNode; std::allocator_traitsstd::allocator<_CharT >::allocator_type = std::allocator]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:520:39: required from 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {TreeNode*&}; _Tp = TreeNode; _Alloc = std::allocator; __gnu_cxx::_Lock_policy _Lp = (gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:615:4: required from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, Tp*, const _Alloc&, _Args&& ...) [with _Tp = TreeNode; _Alloc = std::allocator; _Args = {TreeNode*&}; __gnu_cxx::_Lock_policy _Lp = (_gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr_base.h:1100:35: required from 'std::__shared_ptr<_Tp, Lp>::_shared_ptr(std::_Sp_make_shared_tag, const Alloc&, _Args&& ...) [with _Alloc = std::allocator; _Args = {TreeNode*&}; _Tp = TreeNode; __gnu_cxx::_Lock_policy _Lp = (_gnu_cxx::_Lock_policy)2u]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:319:64: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const Alloc&, _Args&& ...) [with _Alloc = std::allocator; _Args = {TreeNode*&}; _Tp = TreeNode]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:619:14: required from 'std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = TreeNode; _Alloc = std::allocator; _Args = {TreeNode*&}]'
/usr/local/include/c++/6.3.0/bits/shared_ptr.h:635:39: required from 'std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = TreeNode; _Args = {TreeNode*&}]'
Line 18: Char 44: required from here
/usr/local/include/c++/6.3.0/ext/new_allocator.h:120:4: error: invalid conversion from 'TreeNode*' to 'int' [-fpermissive]
{ ::new((void *)_p) Up(std::forward<_Args>(_args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我寻思使用make_shared<>也不会让TreeNode*变成int啊,而且直接使用构造函数就可以,太奇怪了