题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
/* struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *next; TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) { } }; */ #include <cstddef> class Solution { public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { if(pNode->right==NULL) { if(pNode->next==NULL) { return NULL; } TreeLinkNode *n=pNode; pNode=pNode->next; while(pNode->right==n) { n=pNode; if(pNode->next==NULL) { return NULL; } pNode=pNode->next; } return pNode; } else { pNode=pNode->right; while(pNode->left!=NULL) { pNode=pNode->left; } return pNode; } } };