二叉搜索树与双向链表[待加深]

2018-11-04  本文已影响0人  我的天气很好啦

2018/11/4
🍞环境:牛客的编译环境
🍰语言:JavaScript
☕️难点:

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */

function Convert(pRootOfTree)
{
    // write code here
    if(pRootOfTree == null)
        return null;
    var stack = [],
        p = pRootOfTree,
        isFirst = true;
    var pre;
    var root;
    while(p != null || stack.length > 0){
        while(p != null){
            stack.push(p);
            p = p.left;
        }
        p = stack.pop();
        if(isFirst){
            root = p;
            pre = root;
            isFirst = false;
        }else{
            pre.right = p;
            p.left = pre;
            pre = p;
        }
        p = p.right;
    }
    return root;
}
上一篇 下一篇

猜你喜欢

热点阅读