当前位置:网站首页>Binary tree experience
Binary tree experience
2022-07-18 23:30:00 【bugmaker.】
thinking
Get a binary tree problem , First ask yourself two questions :
1、 Whether you can get the answer by traversing the binary tree ? If possible , Use one traverse With external variables , This is called 「 Traverse 」 The mode of thinking .
2、 Whether a recursive function can be defined , Pass the subproblem ( subtree ) The answer to the original question ? If possible , Write the definition of this recursive function , And make full use of the return value of this function , This is called 「 Break down the problem 」 The mode of thinking .
for example : Calculate the maximum depth of the binary tree
1、 Ergodic thinking
// Record the maximum depth
int res = 0;
// Record the depth of the node traversed
int depth = 0;
// The main function
int maxDepth(TreeNode root) {
traverse(root);
return res;
}
// Binary tree traversal framework
void traverse(TreeNode root) {
if (root == null) {
return;
}
// Preamble position
depth++;
if (root.left == null && root.right == null) {
// To the leaf node , Update maximum depth
res = Math.max(res, depth);
}
traverse(root.left);
traverse(root.right);
// Post sequence position
depth--;
}
2、 Break down the problem
On recursion , Learn to define .
Learn to look beyond the code , Make full use of definitions rather than sorting out recursive steps .
// Definition : Enter the root node , Returns the maximum depth of the binary tree
int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
// Using definitions , Calculate the maximum depth of the left and right subtrees
int leftMax = maxDepth(root.left);
int rightMax = maxDepth(root.right);
// The maximum depth of the whole tree is equal to the maximum depth of the left and right subtrees, and the maximum value is taken ,
// Then add the root node itself
int res = Math.max(leftMax, rightMax) + 1;
return res;
}
边栏推荐
- 广州铁骑霸气“公主抱”,安全感拉满!
- ADB common entry instructions
- Compose 轮播图
- leetcode-两数相加
- Mysql存储模型
- Please check that you have a list of Alibaba pioneer open source projects
- Log collection scheme efk
- Three scenarios take you to unit testing
- [CVPR2019] On Stabilizing Generative Adversarial Training with Noise
- 【决策树】使用决策树进行乳腺癌的诊断
猜你喜欢
![[C language] explanation and Simulation Implementation of strlen function](/img/70/8d94366ae19a8ab07979d808663c89.png)
[C language] explanation and Simulation Implementation of strlen function

How does go ensure the order of concurrent reads and writes Memory model

本周牛客SQL题目记录

"Interface automation" software tests the core skills of salary increase and increases salary by 200%

如何设置三线表线条的粗细
![Progress [detailed summary]](/img/8a/7121cfe6cc7b4afa11a4a9187fc9bf.png)
Progress [detailed summary]

ROS create workspace process

Wanzi detailed C language document

7、常见的垃圾回收器

Robotics xm430-w350 pan tilt motor usage record
随机推荐
Buffer Pool 核心原理
pytest接口自动化测试框架 | 接口测试概述
10、摸清JVM运行状况
ngrx store 状态管理
Instance Noise A trick for stabilising GAN training
T-infinite Road
7、常见的垃圾回收器
[development tutorial 3] crazy shell arm function mobile phone - Introduction to the whole board resources
STM32F1与STM32CubeIDE编程实例-W25Q-SPI-Flash驱动
Mysql存储模型
Yiwen teaches you how to design test cases
Lifeguard certificate examination
微信小程序--》小程序简介与工具安装配置
从0开始安装苹果cms及其资源采集和页面部分代码
HiBench生成基准数据集【WordCount为例】
ACM板子
作为一款时序数据库,TDengine 是如何实现并开源其分布式集群功能?
vi编辑器设置自定义快捷键自动生成c语言的main函数
MySQL related commands
Profile encryption