当前位置:网站首页>Sword finger offer 55 - I. depth of binary tree
Sword finger offer 55 - I. depth of binary tree
2022-07-07 22:52:00 【Yes' level training strategy】
subject : Enter the root node of a binary tree , Find the depth of the tree . The nodes that pass from the root node to the leaf node ( Containing root 、 Leaf nodes ) A path to a tree , The length of the longest path is the depth of the tree .
for example : Given binary tree [3,9,20,null,null,15,7],

Return to its maximum depth 3 .
The time for the person who will write this topic is estimated to be a few seconds .
It's actually intuitive , Want to calculate the depth of the tree , The direct idea is DFS, and DFS The realization of is to use recursion to realize perfectly .
The condition of recursive jump is very simple root == null, There is no doubt about this .
If not equal to null, Then the current node is a depth , Then add the depth of the larger of the two subtrees , Is the maximum depth of a real tree .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
A simple question .
https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof
边栏推荐
- Force deduction - question 561 - array splitting I - step by step parsing
- Revit secondary development - Hide occlusion elements
- Blender exchange group, welcome to the water group ~
- Revit secondary development - modify wall thickness
- 行测-图形推理-2-黑白格类
- Line test - graphic reasoning -5- one stroke class
- Visual design form QT designer design gui single form program
- Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
- 变量与常量
- 行测-图形推理-7-相异图形类
猜你喜欢

行测-图形推理-5-一笔画类

Line test - graphic reasoning - 2 - black and white lattice class

行测-图形推理-8-图群类

LeetCode707. Design linked list

How to choose the appropriate automated testing tools?
Redis官方ORM框架比RedisTemplate更优雅

How pyGame rotates pictures

Basic knowledge of linked list

Redis集群安装

Leetcode206. Reverse linked list
随机推荐
C # realizes the communication between Modbus protocol and PLC
Nx10.0 installation tutorial
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
7-18 simple simulation of banking business queue
Unity FAQ (I) lack of references
Remember aximp once Use of exe tool
Amesim2016 and matlab2017b joint simulation environment construction
Line test - graphic reasoning - 6 - similar graphic classes
Blender exchange group, welcome to the water group ~
[environment] pycharm sets the tool to convert QRC into py file
IP network active evaluation system -- x-vision
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
Ligne - raisonnement graphique - 4 - classe de lettres
Debezium series: MySQL tombstone event
Antd date component appears in English
Ren Qian code compilation error modification
vite Unrestricted file system access to
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
How to choose the appropriate automated testing tools?
Variables and constants