解题思路:
(1)方法一:先求出根结点,再借助辅助空间,递归中序遍历,求得下一个位置
/*
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
}
};
*/
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode) {
if(pNode==NULL) return NULL;
auto p = pNode;
while(pNode->next) {
pNode = pNode->next;
}
vector<TreeLinkNode*> v;
int count = 0,index;
inorder(v,pNode,p,count,index);
if(index==v.size()) return NULL;
else return v[index];
}
void inorder(vector<TreeLinkNode*> &v,TreeLinkNode *root,
TreeLinkNode *p,int &count,int &index) {
if(root==NULL) return;
inorder(v,root->left,p,count,index);
v.push_back(root);
count++;
if(root==p) index = count;
inorder(v,root->right,p,count,index);
}
};
(2)剑指offer的思路,待更...