9.1 cc树与图

4.1] 实现一个函数,检查二叉树是否平衡。(任二子树高度差不超过一)

depth of tree node: # edges from root to node
height of tree node: #edges from note to deepest leaf
but, depth of tree == height of tree
Balanced Binary Tree

4.2] 给有向图,找出两个节点之间是否存在路径

  • dfs: easy to write w/ recursion
  • bfs: iterative, more efficient to check shortest path

4.3] 给定一个有序整数数组,元素各不相同且按照升序排列,编写一个算法,创建一个高度最小的二叉查找树

=========================================
btw

class BSTIterator {
public:
    BSTIterator(TreeNode *root) {
        pushAll(root);
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return !s.empty();
    }

    /** @return the next smallest number */
    int next() {
        auto node = s.top();
        s.pop();
        if (node->right) pushAll(node->right);
        return node->val;
    }
private:
    stack<TreeNode*> s;
    void pushAll(TreeNode* root) {
        while (root) {
            s.push(root);
            root = root->left;
        }        
    }
};
/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = BSTIterator(root);
 * while (i.hasNext()) cout << i.next();
 */

4.4】 给定一棵二叉树,设计一个算法,创建含有某一深度上所有结点的链表(比如:若一棵树的深度为D,则会创建出D个链表)

  • any traversal, just MEMO LEVEL!
  • bfs

4.5] Validate Binary Search Tree

    bool validateTree(TreeNode* root, TreeNode*& last) {
        if (!root) return true;
        if (!validateTree(root->left, last)) return false;
        if (last && root->val <= last->val) return false;
        last = root;
        return validateTree(root->right, last);
    }
    bool isValidBST(TreeNode* root) {
        TreeNode* last = nullptr;
        return validateTree(root, last); 
    }

4.7] Lowest Common Ancestor of a Binary Tree

  • O(n)
    bool cover(TreeNode* root, TreeNode* target) {
        if (!root) return false;
        if (root==target) return true;
        return cover(root->left, target) || cover(root->right, target);
    }
    
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root) return nullptr;
        if (root==p || root==q) return root;
        auto lhasp = cover(root->left, p);
        auto lhasq = cover(root->left, q);
        // both at left
        if (lhasp && lhasq) return lowestCommonAncestor(root->left, p, q);
        // only one at left
        if (lhasp || lhasq) return root;
        // none at left
        return lowestCommonAncestor(root->right, p, q);
    }
  • optimized (assume p, q in tree)
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root) return nullptr;
        if (root==p || root==q) return root;
        auto searchl = lowestCommonAncestor(root->left, p, q);
        auto searchr = lowestCommonAncestor(root->right, p, q);
        if (searchl && searchl!=p && searchl!=q) return searchl; // found at lt
        if (searchr && searchr!=p && searchr!=q) return searchr; // found at rt
        if (searchl && searchr) return root; 
        // else if (root==p || root==q) return root;
        else return searchl? searchl : searchr;
        return nullptr;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,792评论 0 33
  • --- knowledge ---# of leaves = # of nonleaves + 1 - Bi...
    陈十十阅读 318评论 0 0
  • 情绪对一个人工作学习生活有着举足轻重的地位,每当你处在一个坏情绪的状态时,基本没有办法做好任何一件事情。而玻璃心就...
    盛夏东南阅读 1,292评论 0 11
  • 今天老鸦分享他的产品观,有一个很核心的观点,他觉得几乎所有成功的产品,都是针对人性的弱点,激发欲望,让用户着迷。比...
    四四四毛阅读 499评论 1 1
  • 移民公园锻炼,偶遇网络彩排,国学少年央视。 圣旨奉天承运,弱冠少年阳光,五月央视驽临。 孝善楼前人涌,移民公园封园...
    冲天农锄草阅读 439评论 4 16