菜雞前端一塊兒學算法(第三天)

https://juejin.im/post/5e4f46cc51882549507b045cnode


二叉樹的遍歷方式

須要樹立一個意識就是好比有一個tree中插入一個值,並非在樹的最後面插入這個節點,而是把這個樹的子節點所有替換爲該節點 下面是js對樹的實現:算法

function TreeNode(val) {
    this.value = val;
    this.left = this.right = null;
}
複製代碼

前序遍歷

首先訪問根節點,而後訪問根節點的左子樹,在訪問根節點的右子樹函數

function DLR(tree) {
    console.log(tree.value)
    if (tree.left) {
        DLR(tree.left)
    } 
    if (tree.right) {
        DLR(tree.right)
    }
}
複製代碼

中序遍歷

首先訪問根節點的左子樹,而後訪問根節點,再訪問根節點右子樹post

function DLR(tree) {
    if (tree.left) {
        DLR(tree.left)
    }
    console.log(tree.value)
    if (tree.right) {
        DLR(tree.right)
    }
}
複製代碼

後序遍歷

首先訪問根節點的左子樹,而後訪問根節點的右子樹,最後訪問根節點學習

function DLR(tree) {
    if (tree.left) {
        DLR(tree.left)
    }
    if (tree.right) {
        DLR(tree.right)
    }
    console.log(tree.value);
}
複製代碼

delete-node-in-a-bst

總結出算法除了套路,例如這道題我一看就知道用遞歸,這也算是進步了。此外還有具體分析這個題,這個二叉樹是排序好的,就是左邊的比右邊小,遞歸this

var deleteNode = function(root, key) {
    let node;
    // 取出子樹在進行排序
    if (root === null) {
        return root;
    }
    if (key === root.val) {
        if (root.left === null) {
            return root.right;
        } else if (root.right === null) {
            return root.left;
        } else {
            node = root.right;
            while (node.left !== null) {
                node = node.left;
            }
            node.left = root.left;
            return root.right;
        }
    } else if (key > root.val) {
        root.right = deleteNode(root.right, key);
    } else {
        root.left = deleteNode(root.left, key)
    }
    return root;
};
複製代碼

lowest-common-ancestor-of-a-binary-tree

若是當前結點root等於NULL,則直接返回NULL
若是root等於p或者q,那這棵樹必定返回p或者q
而後看遞歸左右子樹(由於是遞歸,使用函數後能夠認爲左右子樹已經算出告終果,用left和right表示;
此時若left爲空,那最終結果只要看right; 若right爲空,那最終結果只要看left;
若是left和right都非空,由於只給了p和q兩個結點,都非空,說明一邊一個,所以root是他們的最近公共祖先;
若是left和right都爲空,則返回空




spa

感受用文字不用容易理解能夠用簡單的例子來進行講解code

1
   / \
  2   8
 / \   
3   4   
複製代碼

若是須要求導34和共同祖先,能夠對比思考一下排序

var lowestCommonAncestor = function(root, p, q) {
    if (root === null) return null;
    if (root === p || root === q) {
        return root;
    }

    const left = lowestCommonAncestor(root.left, p, q);
    const right = lowestCommonAncestor(root.right, p, q);
    if (left !== null && right !== null) {
        return root;
    } else if (left !== null) {
        return left;
    } else if (right !== null) {
        return right;
    }
    return null;
};
複製代碼

kth-largest-element-in-an-array

這道題雖然簡單,但好歹是本身實現的遞歸

var findKthLargest = function(nums, k) {
    const sortArr = nums.sort((a, b) => b - a);
    return sortArr[k - 1]
};
複製代碼

動態規劃

動態規劃聽着逼格挺高的,先學習一下,究竟是怎麼回事。 具體的文章參考什麼是動態規劃 就是將複雜的內容進行簡單化,非要給他取一個高大上的名字,平時咱們對斐波那契的求職就體驗了這個

// 1
function fibonacci(n) {
    if (n < 2) {
        return 1;
    }
    return fibonacci(n - 1) + fibonacci(n - 2)
}
// 2
function fibonacci(n) {
    let i = 0;
    const result = {};
    while (i < n) {
        if (i < 2) {
            return 2;
        } else {
            result[i] = result[i - 1] + result[i - 2]
        }
        i++;
    }
    return result[-1]
}
複製代碼

今天看了一會電視劇,學的不是不少,明天早點起牀,開始學習

相關文章
相關標籤/搜索