weixin_40836227
就爱瞎逛
2019-09-18 14:21

leetcode897. 递增顺序查找树 想用js和辅助栈模拟中序遍历,但出错了?

  • javascript

原题https://leetcode-cn.com/problems/increasing-order-search-tree/

var increasingBST = function(root) {
    let s = [];
    let res = new TreeNode();
    let p = res;

    while(s || root){
        if(root){
            s.push(root);
            root = root.left;
        }else{
            let cur = s.pop();
            root = cur.right;//显示这里出错。
            cur.left = null;
            p.right = cur;
            p = p.right;
        }
    }
    return res.right;
}

图片说明

不懂报错的原因... 而且就算把else换成else if(s)也依然报错
感谢大佬们在百忙中解答我的疑问!!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答