二叉搜索树与双向链表 发表于 2018-10-03 | 分类于 数据结构与算法 , 题目汇总 题目描述输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 1234567891011121314151617181920212223242526272829303132333435public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}public class Solution { public TreeNode Convert(TreeNode pRootOfTree) { if(pRootOfTree == null){ return null; } TreeNode left = Convert(pRootOfTree.left); TreeNode p = left; while(p != null && p.right != null){ p = p.right; } if(left != null){ p.right = pRootOfTree; pRootOfTree.left = p; } TreeNode right = Convert(pRootOfTree.right); p = right; while(p != null && p.left != null){ p = p.left; } if(right != null){ right.left = pRootOfTree; pRootOfTree.right = right; } return left != null ? left : pRootOfTree; }}