首先我们引入“线索二叉树”的概念: "A binary tree isthreadedby making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be null point to the inorder predecessor of the node." 线索二叉树的构建方法...
A binary tree isthreadedby making all right child pointers that would normally be null point to the inorder successor of the node (ifit exists), and all left child pointers that would normally be null point to the inorder predecessor of the node. 就是说线索二叉树实际上是把所有原本为空的...
Time Complexity: O(N) Space Complexity: O(N) 递归缓存 Solution1_a Code:Find Inorder Predecessor classSolution{publicTreeNodeinorderPredecessor(TreeNode root,TreeNode p){TreeNode cur=root;TreeNode prede=null;while(cur!=null){if(p.val==cur.val)cur=cur.left;elseif(p.val>cur.val){// tu...
a. predecessor.right = null, 表明左子树还没遍历过。那么predecessor.right 指向 cur; cur = cur.left. b. predecessor.right = cur, 表明左子树已经遍历完了。那么先add cur, 然后把predecessor.right 改回null, 恢复树的结构,然后cur = cur.right. Note:1.while()里的条件和下面的if()条件是相呼应的...