1. 题目
设计一个算法,找出二叉搜索树中指定节点的“下一个”节点(也即中序后继)。
如果指定节点没有对应的“下一个”节点,则返回null。
示例 1:
输入: root = [2,1,3], p = 1
2
/ \
1 3
输出: 2
示例 2:
输入: root = [5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/ \
2 4
/
1
输出: null
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/successor-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 循环写法,中序遍历即可
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
if(!root || !p)
return NULL;
stack<TreeNode*> stk;
TreeNode *tp;
bool found = false;
while(root || !stk.empty())
{
while(root)
{
stk.push(root);
root = root->left;
}
tp = stk.top();
stk.pop();
if(found)
return tp;
if(tp == p)
found = true;
root = tp->right;
}
return NULL;
}
};