面试题17.12.BiNode
二叉树数据结构TreeNode可用来表示单向链表(其中left置空,right为下一个链表节点)。实现一个方法,把二叉搜索树转换为单向链表,要求依然符合二叉搜索树的性质,转换操作应是原址的,也就是在原始的二叉搜索树上直接修改。
返回转换后的单向链表的头节点。
注意:本题相对原题稍作改动
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binode-lcci
/**
* 根 右 左
*/
TreeNode pre = null;
public TreeNode convertBiNode(TreeNode root) {
if(root == null) return null;
convertBiNode(root.right);
TreeNode tmp = root.left;
root.left = null;
root.right = pre;
pre = root;
convertBiNode(tmp);
return pre;
}