AcWing 19. 二叉树的下一个节点
原题链接
中等
作者:
小轩喵灬
,
2025-01-13 11:30:11
,
所有人可见
,
阅读 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode father;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode inorderSuccessor(TreeNode p) {
if (p.right != null) {
TreeNode node = p.right;
while(node.left != null) {
node = node.left;
}
return node;
} else {
while(p.father != null) {
TreeNode father = p.father;
if (father.left == p) {
return father;
}
p = p.father;
}
}
return null;
}
}