当前位置:网站首页>[brother hero July training] day 15: depth first search
[brother hero July training] day 15: depth first search
2022-07-18 07:34:00 【If I were Wen Shuai】
Series articles
【 Brother hero training in July 】 The first 01 God : Array
【 Brother hero training in July 】 The first 02 God : character string
【 Brother hero training in July 】 The first 03 God : Sort
【 Brother hero training in July 】 The first 04 God : greedy
【 Brother hero training in July 】 The first 05 God : Double pointer
【 Brother hero training in July 】 The first 06 God : The sliding window
【 Brother hero training in July 】 The first 07 God : Hashtable
【 Brother hero training in July 】 The first 08 God : The prefix and
【 Brother hero training in July 】 The first 09 God : Two points search
【 Brother hero training in July 】 The first 10 God : An operation
【 Brother hero training in July 】 The first 11 God : matrix
【 Brother hero training in July 】 The first 12 God : Linked list
【 Brother hero training in July 】 The first 13 God : Double linked list
【 Brother hero training in July 】 The first 14 God : Stack
【 Brother hero training in July 】 The first 15 God : Depth-first search
List of articles
One 、 Calculate the value of Boolean binary tree
2331. Calculate the value of Boolean binary tree
leetcode2231 java
class Solution {
public boolean isleaf(TreeNode root) {
return root.left==null&&root.right==null;
}
public boolean evaluateTree(TreeNode root) {
if(root!=null){
if(!isleaf(root)){
boolean L = evaluateTree(root.left);
boolean R = evaluateTree(root.right);
if(root.val==2){
return L|R;
} else {
return L&R;
}
}
return root.val==0?false:true;
}
return false;
}
}
Two 、 Flip the equivalent binary tree
951. Flip the equivalent binary tree
leetcode951 java
class Solution {
public boolean flipEquiv(TreeNode root1, TreeNode root2) {
if(root1==null&&root2==null){
return true;
}
if(root1!=null&&root2!=null){
if(root1.val!=root2.val){
return false;
}
if(flipEquiv(root1.left,root2.left)&&flipEquiv(root1.right,root2.right)){
return true;
};
if(flipEquiv(root1.left,root2.right)&&flipEquiv(root1.right,root2.left)){
return true;
};
return false;
}
return false;
}
}
3、 ... and 、 Find all the farm groups
1992. Find all the farm groups
leetcode1992 java
class Solution {
int[][] land;
int m,n;
int x,y;
int[][] dirs = {
{
1,0},{
0,1}};
public int[][] findFarmland(int[][] land) {
this.land=land;
List<int[]> res = new ArrayList<>();
m = land.length;
n = land[0].length;
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
if(land[i][j]==1){
System.out.printf("%d %d\n",i,j);
int[] ans =dfs(i,j);
res.add(new int[]{
i,j,ans[0],ans[1]});
}
}
}
return res.toArray(new int[0][]);
}
public int[] dfs(int i,int j){
int[] ans = new int[]{
i,j};
for(int[] dir:dirs){
int ni=i+dir[0];
int nj=j+dir[1];
if(ni>=m||nj>=n||land[ni][nj]==0){
continue;
}
land[ni][nj]=0;
int[] dfs = dfs(ni,nj);
ans[0]=Math.max(dfs[0],ans[0]);
ans[1]=Math.max(dfs[1],ans[1]);
}
return ans;
}
}
Four 、 Look for duplicate subtrees
652. Look for duplicate subtrees
leetcode652 java
class Solution {
List<TreeNode> ans;
Map<String,Integer> count;
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
ans = new ArrayList<>();
count = new HashMap<>();
collect(root);
return ans;
}
public String collect(TreeNode root){
if(root==null) return "#";
String series = root.val+","+collect(root.left)+","+collect(root.right);
count.put(series, count.getOrDefault(series,0)+1);
if(count.get(series)==2){
ans.add(root);
}
return series;
}
}
summary
边栏推荐
- for循环的执行顺序
- Dictionary tree (trie tree)
- numpy获取二维数组某一行、某一列
- Penetration test process
- 如何使用HHDBCS对PolarDB for PostgreSQL存储过程进行调试?
- 【使用Win10自带远程连接工具】远程访问并控制【另一台Win10电脑】
- Openharmony module 2 file samgr_ Server parsing (4)
- PolarDB for PostgreSQL的分布式查询引擎是怎样的?
- Getting started with OpenCV ----- vs how to install opencv Library
- OpenHarmony相关知识学习
猜你喜欢

Gift from JRockit: JMC virtual machine diagnostic tool

2022 China mobile maker marathon Internet of things special competition kicks off

缓存穿透、缓存雪崩、缓存击穿?

ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the

OpenHarmony安全模块之AES加密学习

ITSM确保IT服务台敏捷性的5大实践

Firewall ha configuration

A-F Codeforces Round #806 (Div.4) A-F题解及代码

【示波器的基本使用】以及【示波器按键面板上各个按键含义的介绍】

Openharmony related knowledge learning
随机推荐
Markdown in CSDN sets the width of table columns
Pass value, reference and pointer
Openharmony module 2 file samgr_ Server parsing (3)
Preliminary analysis of openharmony module II
CAS与AQS简单理解
2、趋势科技2017校招开发岗试题
(高频面试题)计算机网络
1、华为机试题记录
OpenHarmony相关知识学习
C language: use macro to redefine printf and print [debug debugging information]
Illegal profits exceed one million, and new outlets in the industry are being cracked and eroded
PHP表单数据写入MySQL代码
How to migrate the complex SQL statements used by applications developed for Oracle to polardb for POS
The function of ifndef /define/endif in the header file
来自JRockit的礼物:JMC虚拟机诊断工具
Devsecops R & D security practice - Design
C language: [bit field operation] (colon is used in the structure)
如何使用HHDBCS对PolarDB for PostgreSQL存储过程进行调试?
每日一题:数组中最大数对和的最小值(LeeCode)
What is the distributed query engine of polardb for PostgreSQL?