题解 | #将二叉搜索树变为双向链表#

二叉搜索树与双向链表

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 prev=null;
//中序遍历
public void ConvertChild(TreeNode pCur){
    if(pCur==null) {
        return;
    }
    ConvertChild(pCur.left);
    
    //处理每个节点,将该节点的左右指向改变
    pCur.left=prev;
    if(prev!=null){
        prev.right=pCur;
    }
    prev=pCur;
    
    ConvertChild(pCur.right);
    
    
}
public TreeNode Convert(TreeNode pRootOfTree) {
    if(pRootOfTree ==null) return null;
    ConvertChild(pRootOfTree);
    
    //找到链表的头节点
    TreeNode head=pRootOfTree;
    while(head.left !=null){
        head=head.left;
    }
    return head;
}

}

全部评论

相关推荐

头像
10-27 15:50
门头沟学院 Java
想进开水团喝开水:有一种店 只能外卖 不能堂食 你猜为什么
点赞 评论 收藏
分享
10-31 22:23
门头沟学院 Java
天然不是卷王:太好了 佬的金九银十结束,等offer吐出来,我的金11银12就要开始了
点赞 评论 收藏
分享
12-03 15:20
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务