当前位置:网站首页>Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
2022-08-04 11:13:00 【lonelyMangoo】
递归
To solve these two problems, I would like to first understand the idea of recursion,There are three elements to solving recursive problems:
- 方法参数和返回值
- 方法参数:There are two cases for method parameters
- If it is related to the previous,Then you have to set it as a global variable
例如刷题中的538题,就会出问题,As a result, the value passed in is not the latest,下面的543the same question. - If it doesn't matter before,That is to say, it only works on the current layer,即局部的,It is passed through the method body
- If it is related to the previous,Then you have to set it as a global variable
- 返回值
When you need to use the return value,设置返回值,The following two questions will be required,
而上面提到的刷题中的538题,已经使用sumThe values I need are logged,No return value operation is required.另外,Generally judged with return value,有false直接返回了.
- 终止条件
It's where you need to be clear,When to stop and when to end. - 函数体
Operations on nodes and data
下面的题目,I will go through the above three steps to analyze
543. 二叉树的直径
543. 二叉树的直径
这道题翻译一下,Find the largest of the sum of the heights of the left and right subtrees of each node.
Why grab every node,Because I did it with hierarchical traversal at first,I thought it must start from the follow node,这个想法是错的,举个例子,The height of the root's right subtree is 1,The left subtree is two subtrees of the same height,高度都为10.
代码:
//It is written outside because this is a global variable,Find the global largest.
int ans;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return ans;
}
//The return value is set because it is needed to find the height
private int depth(TreeNode root){
//结束条件
if(root == null){
return 0;
}
//方法体
// Recursively find the height of the left and right subtrees of the current node
int leftDepth = depth(root.left);
int rightDepth = depth(root.right);
ans = Math.max(ans,leftDepth+rightDepth);
//返回当前子树的高度
return Math.max(leftDepth,rightDepth)+1;
}
617. 合并二叉树
617. 合并二叉树
题目很好理解,思路,Pick up new ones
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
return build(root1, root2);
}
//先序遍历.
//The return value is set because it is needed to build a tree
public static TreeNode build(TreeNode root1, TreeNode root2){
//终止条件
//叶子结点,直接返回
if(root1==null && root2==null) return null;
//And wool scallops,可冲
if(root1!=null && root2==null) return root1;
if(root1==null && root2!=null) return root2;
//The overlapping part of the left and right subtrees,需要叠加,
TreeNode node = new TreeNode(root1.val + root2.val);
//建树,Here you can see the return value that needs to be used.
node.left=build(root1.left, root2.left);
node.right=build(root1.right,root2.right);
return node;
}
边栏推荐
- Jenkins User Manual (1) - Software Installation
- Using .NET to simply implement a high-performance clone of Redis (2)
- 解析treeSet集合进行自定义类的排序
- datax oracle to oracle incremental synchronization
- 面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服
- cat /proc/kallsyms 发现内核符号表值都为0
- 什么是终端特权管理
- zabbix deployment
- 什么是 DevOps?看这一篇就够了!
- 使用.NET简单实现一个Redis的高性能克隆版(二)
猜你喜欢
MATLAB程序设计与应用 3.2 矩阵变换
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
Advanced transcriptome analysis and R data visualization hot registration (2022.10)
What is the terminal privilege management
数字知识库及考学一体化平台
萌宠来袭,如何让“吸猫撸狗”更有保障?
【机器学习】:如何对你的数据进行分类?
使用.NET简单实现一个Redis的高性能克隆版(二)
图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
随机推荐
【LeetCode】1403.非递增顺序的最小子序列
数字知识库及考学一体化平台
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
萌宠来袭,如何让“吸猫撸狗”更有保障?
C language * Xiaobai's adventure
RL78开发环境
Advanced transcriptome analysis and R data visualization hot registration (2022.10)
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
Learn to use the basic interface of set and map
数据化管理洞悉零售及电子商务运营——零售密码
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
使用.NET简单实现一个Redis的高性能克隆版(二)
The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
深度强化学习与APS的一些感想
第二批养老理财试点产品发行 一小时销售20亿元
win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
Small program containers accelerate the construction of an integrated online government service platform
面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服
ArrayList和LinkedList的区别
zabbix deployment