https://www.bilibili.com/video/av91129831
栈做https://www.nowcoder.com/profile/6084631/codeBookDetail?submissionId=15463248
队列做https://www.nowcoder.com/profile/1763495/codeBookDetail?submissionId=16862302
数组:https://www.nowcoder.com/profile/5530542/codeBookDetail?submissionId=14998267
class Solution {
public:
TreeNode* convert(TreeNode* root)
{
if(!root) return NULL;
vector<TreeNode* > vec;
cout<<"&vec="<<&vec<<endl;
dfs(root,vec);
for(int i =0;i < vec.size()-1;i++)
{
vec[i]->right=vec[i+1];
vec[i+1]->left=vec[i];
}
vec[0]->left=vec[vec.size()-1]->right=NULL;
return vec[0];
}
void dfs(TreeNode* root,vector<TreeNode* > &vec)
{
cout<<"&vec2="<<&vec<<endl;
if(!root) return;
dfs(root->left,vec);
vec.push_back(root);
dfs(root->right,vec);
}
};
可以看到convert(TreeNode* root) 中的
vector<TreeNode* > vec;和
void dfs(TreeNode* root,vector<TreeNode* > &vec)的
&vec地址相同