题解 | #从上往下打印二叉树#

二叉搜索树与双向链表

http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

这个问题就是词汇表中序遍历加链表操作(有点像反转链表,要先搞一个pre指针)

function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
}
let pre=new TreeNode(0)
function Convert(pRootOfTree)
{
    // write code here中序遍历
    let head=pRootOfTree
    if(!head){
        return null
    }
    while(head.left){
        head=head.left
    }
    inOrder(pRootOfTree)
    head.left=null
    return head 
 
    
}
function inOrder(cur){
    if(!cur) return null;
    inOrder(cur.left)
    pre.right=cur
    cur.left=pre 
    pre=cur
    inOrder(cur.right)  
}
module.exports = {
    Convert : Convert
};
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务