Skip to content

【082-week2】 -二叉树前两道题的理解 #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
IMC666 opened this issue Apr 27, 2019 · 0 comments
Open

【082-week2】 -二叉树前两道题的理解 #321

IMC666 opened this issue Apr 27, 2019 · 0 comments

Comments

@IMC666
Copy link
Contributor

IMC666 commented Apr 27, 2019

  1. Second Minimum Node In a Binary Tree:https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
    关键字:second minimum
    分析:
    这是一棵奇怪的树,父节点是大于等于子节点的,而且子节点要么没有要么两个,现在是要去找到第二小的节点。因为是寻求第二小的元素,遍历肯定是必须的。要利用树的结构,肯定就需要递归。因为是要在寻求第二小的节点,意味着只需要维持容量为二的队列并且时刻保证它按照(从小到大的顺序)【队列相对栈更好维持】。
  2. Lowest Common Ancestor of a Binary Tree:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
    关键字:lowest common ancestor
    分析:
    寻找最近共同祖先!最鸡贼的办法应该是直接利用完全二叉树的性质来求解,通过将所给树填进数组(改造层序遍历),然后开始迭代寻找。也可以填进HashMap(改造前序遍历),也可利用递归遍历,从根节点到两个目标节点具有两条唯一的路径,将其储存下来,对比即可找到最近的祖先。「复杂度是N的平方」也可以先各自找到一次,记录下深度,从深度小的开始网上逐个测试搜索另一个目标节点,但私以为这是最蠢的。最后还是打算用递归,递归里面嵌套递归,应该是还可以再优化的。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant