
深度优先搜索
枫流仁武
这个作者很懒,什么都没留下…
展开
-
LeetCode 538 把二叉树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。中序遍历的变化版本from collections import dequeclass Solution: def convertBST(self, root: TreeNode) -> TreeNode: if root is None: return root原创 2020-09-21 07:49:31 · 135 阅读 · 0 评论 -
LeetCode 785 判断二分图
给定一个无向图graph,当这个图为二分图时返回true。from typing import *from collections import defaultdictclass Solution: """ 利用染色的方法判断一个图是否是二分图 """ def __init__(self): self.flag = True self.colors = None self.graph = None d原创 2020-09-19 08:24:53 · 135 阅读 · 0 评论 -
LeetCode 297 二叉树的序列化和反序列化
这个题难度其实不大,利用深度优先遍历构建字符串,同时也利用深度遍历返回树。class Codec: def __init__(self): self.idx = 0 def serialize(self, root): """Encodes a tree to a single string. :type root: TreeNode :rtype: str """ p = root原创 2020-06-16 08:23:03 · 95 阅读 · 0 评论 -
PAT 1053 Path of Equal Weight
Given a non-empty tree with rootR, and with weightWiassigned to each tree nodeTi. Theweight of a path fromRtoLis defined to be the sum of the weights of all the nodes along the path fro...原创 2019-09-05 11:40:32 · 82 阅读 · 0 评论 -
PAT 1131 Subway Map
In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with ...原创 2019-08-22 17:37:05 · 118 阅读 · 0 评论 -
PAT 1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. Asocial clusteris a set of people who have some of thei...原创 2019-06-22 19:25:31 · 95 阅读 · 0 评论 -
PAT 1034 Head of a Gang
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatAandBis related. The weight of a relation is defined to be t...原创 2019-08-21 19:35:37 · 88 阅读 · 0 评论 -
PAT 1004 Counting Leaves
#include <iostream>#include <vector>#include <climits>#include <cstring>using namespace std;vector<int> vector1[105];//代表某个节点的叶子节点int nums[105];//代表每一层的叶子节点的个数int...原创 2019-07-22 15:42:20 · 91 阅读 · 0 评论 -
PAT 1021 Deepest Root
这道题第一遍做的时候没什么思路,现在再做感觉不难...没必要考虑太多,只有叶子节点才有可能成为最深的节点,只要判断所有叶子节点就可以了。注意有一个测试节点N为1,需要特殊考虑#include <iostream>#include <vector>#include <map>#include <cstring>using na...原创 2019-07-20 19:25:06 · 109 阅读 · 0 评论 -
PAT 1106 Lowest Price in Supply Chain
简单的深度优先搜索#include <iostream>#include <map>#include <vector>#include <climits>using namespace std;map<int,vector<int>> graph;int minDepth;int countMin=0;u...原创 2019-06-23 16:36:31 · 108 阅读 · 0 评论 -
PAT 1103 Integer Factorization
这道题值得反复看深度优先搜索解决#include <iostream>#include <cmath>#include <vector>#include <algorithm>using namespace std;vector<vector<int>> res;bool cmp(vector<in...原创 2019-06-23 16:15:39 · 95 阅读 · 0 评论 -
PAT 1079 Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on...原创 2019-06-09 12:24:07 · 304 阅读 · 0 评论