题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree) {
convertLeft(pRootOfTree);
return head;
}
public void convertLeft(TreeNode root){
if(root == null){
return ;
}
root.left = pre;
pre.right = root;
}else{
head = root;
}
count++;
pre = root;
convertLeft(root.right);
}
}
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode pre = null;
int count = 0;
//记录第一个头结点
TreeNode head = null;public TreeNode Convert(TreeNode pRootOfTree) {
convertLeft(pRootOfTree);
return head;
}
public void convertLeft(TreeNode root){
if(root == null){
return ;
}
convertLeft(root.left);
//第一个头结点不需要左指针,
if(count != 0){root.left = pre;
pre.right = root;
}else{
head = root;
}
count++;
pre = root;
convertLeft(root.right);
}
}