题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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 {
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) {
return null ;
}
TreeNode l = pRootOfTree.left ;
TreeNode r = pRootOfTree.right ;
pRootOfTree.left = null ;
pRootOfTree.right = null ;
TreeNode Lh = Convert(l) ;
TreeNode Rh = Convert(r) ;
TreeNode cur = Lh ;
while(cur != null && cur.right != null) {
cur = cur.right ;
}
pRootOfTree.left = cur ;
if(cur != null) {
cur.right = pRootOfTree ;
}
pRootOfTree.right = Rh ;
if(Rh != null) {
Rh.left = pRootOfTree ;
}
return Lh == null ? pRootOfTree : Lh;
}
}
public 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 l = pRootOfTree.left ;
TreeNode r = pRootOfTree.right ;
pRootOfTree.left = null ;
pRootOfTree.right = null ;
TreeNode Lh = Convert(l) ;
TreeNode Rh = Convert(r) ;
TreeNode cur = Lh ;
while(cur != null && cur.right != null) {
cur = cur.right ;
}
pRootOfTree.left = cur ;
if(cur != null) {
cur.right = pRootOfTree ;
}
pRootOfTree.right = Rh ;
if(Rh != null) {
Rh.left = pRootOfTree ;
}
return Lh == null ? pRootOfTree : Lh;
}
}
一个菜鸟的算法刷题记录 文章被收录于专栏
分享一个菜鸟的成长记录