2_a: Find Inorder Predecessor 2_b: Find Inorder Successor: byjeantimex: let's take the successor for example, basically we always want to find p's closest node (in inorder traversal) and the node's value is larger than p's value, right? That node can either be p's parent or th...
Predecessor 1publicclassSolution {2publicTreeNode inorderSuccessor(TreeNode root, TreeNode p) {3if(root ==null) {4returnnull;5}6if(root.val >=p.val) {7returninorderSuccessor(root.left, p);8}else{9TreeNode right =inorderSuccessor(root.right, p);10returnright ==null?root : right;11}...
// root.val > p.val. In this case, root can be a possible answer, so we store the root node first and call it res. However, we don't know if there is anymore node on root's left that is larger than p.val. So we move root to its left and check again. // root.val <= ...