既上篇关于二叉搜索树的文章 后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历。
线索二叉树介绍
首先我们引入“线索二叉树”的概念:
"A binary tree is threaded by 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."
线索二叉树的构建方法:
consider a node k that has a right child r. Then the left pointer of r must be either a child or a thread back to k. In the case that r has a left child, that left child must in turn have either a left child of its own or a thread back to k, and so on for all successive left children.
线索二叉树由A. J. Perlis和 C. Thornton发明,他们利用二叉树中那些没有左孩子或者右孩子(或都没有)的结点,将它们的left 和 right指针利用起来,作为辅助指针,指向上层的某一个结点。
这种新的遍历方式的基本思路: 遍历的过程会不断给一些NULL的指针赋值,使其成为辅助指针,来构建线索二叉树;同时也会不断将不再需要的辅助指针置回NULL。完成遍历后,树的结构不会改变。
在实际遍历过程中,辅助指针并不需要left, right都赋值,可以只赋值right或者只赋值 left,就达到中序遍历全部结点的效果。
下面是只赋值right 作为辅助指针完成中序遍历的伪代码:
while(cur != NULL){ if(cur -> left == NULL) output cur; else{ pre = cur; TreeNode* lchild = pre -> left; TreeNode* node = find right most child of lchild, or right child which equals to pre; if(right most child of lchild){ //此时需要构造辅助指针,放入node的right指针中 node -> right = pre; cur = pre -> left; //搜寻下一级left child }else{ //若辅助指针已经被构造过,删除辅助指针,输出辅助指针所指向的结点,也就是pre node -> right = NULL; output pre; cur = pre -> right; //搜寻下一级right child } } }
这个遍历方式也是LeetCode中 Binary Tree Inorder Traversal 一题的解法之一。
附题目,Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
实现代码,输出部分高亮表示。
vector<int> inorderTraversal(TreeNode *root) { vector<int> v; if(NULL == root) return v; TreeNode *cur, *pre; cur = root; while(cur){ if(!cur -> left){ v.push_back(cur -> val); cur = cur -> right; }else{ pre = cur; cur = cur -> left; while(cur -> right && cur -> right != pre){ cur = cur -> right; } if(!cur -> right){ cur -> right = pre; cur = pre -> left; }else{ cur -> right = NULL; v.push_back(pre -> val); cur = pre -> right; } } } return v; }
应用
介绍完了这种遍历方式,我们谈谈它的引申应用。
我们依旧以上篇博文中的第二题为例,但是这一次,加了一个额外的要求:空间复杂度必须为constant。
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
上篇博文中的解题方法是使用递归进行中序遍历,但是递归的调用使得程序需要额外的栈空间。
解题的思路依然是中序遍历,遍历的同时进行比较preTrav和current结点的值。只不过这里中序遍历的方式变成了构建和删除线索二叉树。
将上面的代码做一些修改,把存到vector的部分变成比较和存储结点(高亮部分),就完成了。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void recoverTree(TreeNode *root) { if(!root) return; TreeNode *wnd1 = NULL, *wnd2 = NULL; TreeNode *cur = root, *pre = NULL, *preTrav = NULL; while(cur){ if(!cur -> left){ //record nodes to swap if(preTrav && (preTrav -> val > cur -> val)){ if(!wnd1){ wnd1 = preTrav; } wnd2 = cur; } preTrav = cur; cur = cur -> right; }else{ pre = cur; cur = cur -> left; while(cur -> right && cur -> right != pre){ cur = cur -> right; } if(!cur -> right){ cur -> right = pre; cur = pre -> left; }else{ cur -> right = NULL; //record nodes to swap if(preTrav && (preTrav -> val > pre -> val)){ if(!wnd1){ wnd1 = preTrav; } wnd2 = pre; } preTrav = pre; cur = pre -> right; } } } int temp = wnd1 -> val; wnd1 -> val = wnd2 -> val; wnd2 -> val = temp; } };
当然,其实这种思路依然是需要额外空间的:那些用来完成线索二叉树的指针,虽然之后被删除,但是会临时占用一部分空间。所以,严格意义上说,这也不是Constant的空间复杂度解法。但是,权且作为一种新的思路,供大家参考。
参考:
水中的鱼 [LeetCode] Recover Binary Search Tree 解题报告
《数据结构(C++描述)》,金远平编著,清华大学出版社 - 4.5.2 中序遍历线索二叉树
http://en.wikipedia.org/wiki/Threaded_binary_tree