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. 就是说线索二叉树实际上是把所有原本为空的...
线索二叉树介绍 首先我们引入“线索二叉树”的概念: "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." 线索...
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){// turn rightprede=cur;cur=cur.right;}elsecur=cur.left;}ret...
2. cur.left 不为 null, 先在左子树中找到cur 的predecessor, 然后分两种情况考虑: a. predecessor.right = null, 表明左子树还没遍历过。那么predecessor.right 指向 cur; cur = cur.left. b. predecessor.right = cur, 表明左子树已经遍历完了。那么先add cur, 然后把predecessor.right 改回null, 恢复树...