1.5 LeetCode(剑指offer)

132 阅读1分钟

36.二叉搜索树与双向链表

// 参考别人的代码写出来的,需要注意head指针的切入点,以及遍历结束后,首位相连的问题。
var treeToDoublyList = function(root) {
    if(!root) return root;
    let head = null;
    let cur = null;
    const helper = root=>{
        if(!root) return;
        helper(root.left);
        if(!head){
            head = root;
        }
        else{
            cur.right = root;
            root.left = cur;
        }
        cur = root;
        helper(root.right);
    }
    helper(root);
    head.left = cur;
    cur.right = head;
    return head;
};