题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

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;
    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);
        
    }
   
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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