ARTS 2019 03 31(24)

68 篇文章 0 订阅
49 篇文章 0 订阅

ARTS
Algorithm:每周至少做一个leetcode的算法题;
Review:阅读并点评至少一篇英文技术文章;
Tip/Tech:学习至少一个技术技巧;
Share:分享一篇有观点和思考的技术文章;

Algorithm

235. 二叉搜索树的最近公共祖先

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
这道题有个最简单的解法,就是直接吧到两个节点的路径记录下来,然后进行比较,思路非常加单易懂。

“Talk is cheap. Show me the code.” ― Linus Torvalds

Queue<TreeNode> queueFirst = new LinkedList<>();
    Queue<TreeNode> queueSecond = new LinkedList<>();
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        TreeNode nodeFirst = root;
        TreeNode nodeSecond = root;
        while (nodeFirst != null || nodeSecond != null) {
            if (p.val == nodeFirst.val) {
                queueFirst.offer(nodeFirst);
            } else if (p.val > nodeFirst.val) {
                queueFirst.offer(nodeFirst);
                nodeFirst = nodeFirst.right;
            } else {
                queueFirst.offer(nodeFirst);
                nodeFirst = nodeFirst.left;
            }

            if (q.val == nodeSecond.val) {
                queueSecond.offer(nodeSecond);
            } else if (q.val > nodeSecond.val) {
                queueSecond.offer(nodeSecond);
                nodeSecond = nodeSecond.right;
            } else {
                queueSecond.offer(nodeSecond);
                nodeSecond = nodeSecond.left;
            }
            if (p.val == nodeFirst.val && q.val == nodeSecond.val) {
                break;
            }
        }
        TreeNode res = new TreeNode(0);
        while (queueFirst.size() > 0 && queueSecond.size() > 0) {
            TreeNode tempFirst = queueFirst.poll();
            TreeNode tempSecond = queueSecond.poll();
            if (tempFirst.val == tempSecond.val) {
                res = tempFirst;
            } else {
                break;
            }
        }
        return res;
    }
236. 二叉树的最近公共祖先

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
这题比上一题难一点,因为这个不是二叉搜索树,没有那种左小右大的特点,所有找起点来有点费劲。
我的思路是
(1)先遍历一遍所有的树的节点,注意这里遍历的方式最好是先序遍历,把所有的经的点放入List中。
(2)然后在list里面把第一个点P的路径找出来。然后把点放入集合Set当中.
(3)接着在寻找第二个点Q,逐步往上寻找,遇到的第一个在P的路径集合中的点就退出。

“Talk is cheap. Show me the code.” ― Linus Torvalds

List<TreeNode> list = new ArrayList<>();
Set<TreeNode> pathP = new HashSet<TreeNode>();
TreeNode result = null;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    allPath(root);
    searchP(p);
    searchQ(q);
    return result;
}

public void searchP(TreeNode p) {
    int indexP = list.indexOf(p);
    TreeNode preNode = p;
    pathP.add(p);
    for (int i = indexP; i >= 0; --i) {
        TreeNode tempNode = list.get(i);
        if (tempNode.left == preNode || tempNode.right == preNode) {
            pathP.add(tempNode);
            preNode = tempNode;
        }
    }
}

public void searchQ(TreeNode q) {
    int indexQ = list.indexOf(q);
    TreeNode preNode = q;        
    for (int i = indexQ; i >= 0; --i) {
        TreeNode tempNode = list.get(i);
        if (tempNode == preNode || tempNode.left == preNode || tempNode.right == preNode) {
            if (pathP.contains(tempNode)) {
                result = tempNode;
                return;
            }
            preNode = tempNode;
        }
    }
    result = q;
}

public void allPath(TreeNode node) {
    if (node == null) {
        return;
    }
    list.add(node);
    allPath(node.left);
    allPath(node.right);
}

Review

https://www.techiedelight.com/find-size-largest-bst-in-binary-tree/

Find the size of the largest BST in a Binary Tree

寻找二叉树中的二叉搜索树的最大容量。第一种就是硬解,判断每个节点是不是二叉搜索树的根节点,如果是,那么计算每个子树的容量大小。这样的效率很低,但是最后那个比较好的做法我居然没有参透啊
好像是根据遍历一颗书去构造一个二叉树的对象。然后记录下来容量,感觉有点像动态规划?

Tip/Tech

尝试用『艾森豪威尔矩阵』来分析身边的一些事情。
在这里插入图片描述
发现知道是一回事,知道如何去使用又是另一回事了。

Share

《浪潮》
在浪潮电影中,我看到了极权注意是如何一步一步的变成现实的,如果没有控制好形势,那么非常容易失控,导致各种各样的问题。一个老师在一群学生当中做了一个实验,看看能不能在短期构造出一个纳粹组织的雏形。最后事情走向了失控的地步。电影根据真实的时间改编,而来,真实的事情发生在美国。后来引起了极大的重视。其实法西斯离我们并不遥远。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值