
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
History is littered with hundreds of conflicts over the future of a community, group, location or business that were "resolved" when one of the parties stepped ahead and destroyed what was there. With the original point of contention destroyed, the debates would fall to the wayside. Archive Team believes that by duplicated condemned data, the conversation and debate can continue, as well as the richness and insight gained by keeping the materials. Our projects have ranged in size from a single volunteer downloading the data to a small-but-critical site, to over 100 volunteers stepping forward to acquire terabytes of user-created data to save for future generations.
The main site for Archive Team is at archiveteam.org and contains up to the date information on various projects, manifestos, plans and walkthroughs.
This collection contains the output of many Archive Team projects, both ongoing and completed. Thanks to the generous providing of disk space by the Internet Archive, multi-terabyte datasets can be made available, as well as in use by the Wayback Machine, providing a path back to lost websites and work.
Our collection has grown to the point of having sub-collections for the type of data we acquire. If you are seeking to browse the contents of these collections, the Wayback Machine is the best first stop. Otherwise, you are free to dig into the stacks to see what you may find.
The Archive Team Panic Downloads are full pulldowns of currently extant websites, meant to serve as emergency backups for needed sites that are in danger of closing, or which will be missed dearly if suddenly lost due to hard drive crashes or server failures.
Given the root of a binary tree, then value
v
and depthd
, you need to add a row of nodes with valuev
at the given depthd
. The root node is at depth 1.The adding rule is: given a positive integer depth
d
, for each NOT null tree nodesN
in depthd-1
, create two tree nodes with valuev
asN's
left subtree root and right subtree root. AndN's
original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depthd
is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root's left subtree.Example 1:
Example 2:
Note:
这道题让我们给二叉树增加一行,给了需要增加的值,还有需要增加的位置深度,题目中给的例子也比较能清晰的说明问题。但是漏了一种情况,那就是当d=1时,这该怎么加?这时候就需要替换根结点了。其他情况的处理方法都一样,这里博主第一映像觉得应该用层序遍历来做,每遍历完一层,d自减1,当d==1时,需要对于当前层的每一个结点,先用临时变量保存其原有的左右子结点,然后新建值为v的左右子结点,将原有的左子结点连到新建的左子结点的左子结点上,将原有的右子结点连到新建的右子结点的右子结点,是不是很绕-.-|||。如果d不为1,那么就是层序遍历原有的排入队列操作,记得当检测到d为0时,直接返回,因为添加操作已经完成,没有必要遍历完剩下的结点,参见代码如下:
解法一:
虽然博主一贯的理念是二叉树问题肯定首选递归来解,但是这道题博主刚开始以为递归没法解,结果看了大神们的帖子,才发现自己还是图样图森破,难道二叉树的问题皆可递归?反正这道题是可以的,而且写法 so 简洁,乍一看上去,会有疑问,题目中明明d的范围是从1开始的,为何要考虑d为0的情况,后来读懂了整个解法后,才为原作者的聪慧叹服。这里d的0和1,其实相当于一种 flag,如果d为1的话,那么将 root 连到新建的结点的左子结点上;反之如果d为0,那么将 root 连到新建的结点的右子结点上,然后返回新建的结点。如果 root 存在且d大于1的话,那么对 root 的左子结点调用递归函数,注意此时若d的值正好为2,那么就不能直接减1,而是根据左右子结点的情况分别赋值1和0,这样才能起到 flag 的作用嘛,叼的飞起,参见代码如下:
解法二:
Github 同步地址:
#623
参考资料:
https://leetcode.com/problems/add-one-row-to-tree/
https://leetcode.com/problems/add-one-row-to-tree/discuss/104555/C%2B%2B-Java-10-line-Solution-no-helper
https://leetcode.com/problems/add-one-row-to-tree/discuss/104547/Java-three-methods-one-BFS-and-two-DFS
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: