<Tree> 337 BFS: 103】的更多相关文章

337. House Robber III 每个结点有两个结果: 1. result[ 0 ], 不偷,  需要加上子节点的最大值,left[ 0 ] , left[ 1 ] 的最大值 + right[ 0 ], right[ 1 ] 2. result[ 1 ], 偷, 加上子节点不偷的值, left[ 0 ] + right[ 0 ] + 本节点 . val class Solution { public int rob(TreeNode root) { int[] result = rob…
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \  …
E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tree is a connected undirected graph consisting of n vertices and n  -  1 edges. Vertices are numbered 1 through n. Limak is a little polar bear. He once…
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-G.html 题目传送门 - 2018牛客多校赛第三场 G 题意 给定一个 $n$ 个节点的树,有 $k$ 种颜色. 现在让你给每一个节点都染上一种颜色,总共有 $k^n$ 种方法. 现在问,在所有染色方案中,使得相同颜色点对之间的最短距离为 $D$ 的有多少种方案. 答案对于 $10^9+7$ 取模. $n,k,d\leq 5000$ 题解 首先我们来算…
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \   15   7 return its level order traversal as: [  [3], …
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.  Return a list of all possible strings we could create. Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2",…
Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'. Example An…
BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或搜索算法,在树与图相关算法的考察中是非常常见的两种解题思路. BFS与DFS常考算法整理 Definition of DFS and BFS How to Implement DFS and BFS DFS How to explore as far as possible How to backt…
由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含在一个topic中,就以我自己做的先后顺序为准了. Array ---11.Container With Most Water 给定许多条与y轴平行的直线,求其中两条直线与x轴围成的容器的最大容量. 这道题用到了双指针的思想.我们在数轴的两端分别放置一个left指针和right指针,因为容器容量=较短边*两边…
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorted Array II 277 Find the Celebrity 189 Rotate Array 41 First Missing Positive 299 Bulls and Cows 134 Gas Station 118 Pascal's Triangle 很少考 119 Pascal's…