剑指offer-26-二叉搜索树与双向链表
二叉搜索树与双向链表
http://www.nowcoder.com/questionTerminal/947f6eb80d944a84850b0538bf0ec3a5
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
TreeNode* Convert(TreeNode* pRootOfTree) { if(!pRootOfTree) return nullptr; TreeNode *pre = nullptr; TreeNode *root = pRootOfTree; ConvertDfs(pRootOfTree, pre, root); return root; } void ConvertDfs(TreeNode *cur, TreeNode *&pre, TreeNode *&root) { if(!cur) return; ConvertDfs(cur->left, pre, root); cur->left = pre; if(pre) pre->right = cur; else root = cur; pre = cur; ConvertDfs(cur->right, pre, root); }