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

二叉搜索树与双向链表

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
收藏
分享

创作者周榜

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