当前位置:网站首页>[binary number] maximum depth of binary tree + maximum depth of n-ary tree
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
2022-04-23 17:36:00 【Alson_ Code】
One 、 Title Description
Link to the original text :104. The maximum depth of a binary tree
A detailed description :
The depth of a binary tree is the number of nodes in the longest path from the root node to the farthest leaf node .
explain : A leaf node is a node that has no children .
Example :
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
Return to its maximum depth 3 .
Two 、 Thought analysis
Once before The maximum depth of the binary number , In fact, it uses hierarchical traversal ! This time we use recursion !
First of all, let's understand a sentence : Preorder traversal is depth , Post order traversal is depth , Depth equals height !
From this sentence, we can see two solutions , A preamble , A post order !
Let's talk about the sequence first ( Recursive Trilogy ):
- Determine the parameters and return values , The return value is int Depth of type , Parameters are subtrees + depth
- Termination conditions , If the subtree is null Returns the current depth
- Single layer logic is , Traverse the left node , After traversing the right node , Compare the size !
Let's talk about the preface ( Recursive Trilogy ):
- Because the first root is on the left and right , So you can't return a value at the root , So the return value is void, Parameter or subtree + depth
- The termination condition is to find that if the left and right subtrees are empty, it will end
- Compare with the current depth and root to get the maximum value , Then recursively obtain the maximum value in the left and right subtrees !
3、 ... and 、AC Code
After the sequence traversal :
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public int maxDepth(TreeNode root) {
return getDepth(root, 0);
}
public int getDepth(TreeNode root, int depth){
if (root == null) return depth;
int leftDepth = getDepth(root.left, depth + 1);// Left
int rightDepth = getDepth(root.right, depth + 1);// Right
return leftDepth > rightDepth ? leftDepth : rightDepth;// root
}
}
The former sequence traversal :
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
int result = 0;
public int maxDepth(TreeNode root) {
if (root == null) return result;
getDepth(root, 1);
return result;
}
public void getDepth(TreeNode root, int depth){
result = result > depth ? result : depth;// root
if (root.left != null){
depth++;
getDepth(root.left, depth);
depth--;// Return to the original value , Otherwise, the right subtree will be found on the basis of the left subtree !
}
if (root.right != null){
depth ++;
getDepth(root.right, depth);
depth--;
}
}
}
Four 、 summary
- depth = Height , Depth is from root to leaf node , Height is from leaf to root !
5、 ... and 、 Consolidation exercises
/* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; } }; */
class Solution {
public int maxDepth(Node root) {
return getDepth(root, 1);
}
public int getDepth(Node root, int depth){
if (root == null) return 0;
int maxDepth = depth;
for (int i = 0; i < root.children.size(); i++){
int tmpDepth = getDepth(root.children.get(i), depth + 1);
maxDepth = tmpDepth > maxDepth ? tmpDepth : maxDepth;
}
return maxDepth;
}
}
Thanks for reading , I am a Alson_Code, One likes to complicate simple problems , A program that simplifies complex problems !
版权声明
本文为[Alson_ Code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231728041597.html
边栏推荐
- Header built-in object
- In ancient Egypt and Greece, what base system was used in mathematics
- stm32入门开发板选野火还是正点原子呢?
- C语言程序设计之函数的构造
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- [二叉数] 二叉树的最大深度+N叉树的最大深度
- 386. 字典序排数(中等)-迭代-全排列
- Deep understanding of control inversion and dependency injection
- Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
- ASP. Net core reads the configuration file in the class library project
猜你喜欢
![Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers](/img/65/89473397da4217201eeee85aef3c10.png)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers

1217_使用SCons生成目标文件

Understanding of RPC core concepts

PC电脑使用无线网卡连接上手机热点,为什么不能上网

C语言函数详解

Allowed latency and side output
Compare the performance of query based on the number of paging data that meet the query conditions

EF core in ASP Generate core priority database based on net entity model

RPC核心概念理解

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
随机推荐
[WPF binding 3] listview basic binding and data template binding
198. Looting - Dynamic Planning
Understanding and small examples of unity3d object pool
C语言程序设计之函数的构造
Understanding of RPC core concepts
470. 用 Rand7() 实现 Rand10()
How to change input into text
C dapper basically uses addition, deletion, modification and query transactions, etc
. net type transfer
. net cross platform principle (Part I)
剑指 Offer 03. 数组中重复的数字
Detailed explanation of C webpai route
Use of todesk remote control software
古代埃及希腊,数学用的什么进制
HCIP第五次实验
01-初识sketch-sketch优势
STM32 entry development board choose wildfire or punctual atom?
tidb-server 的配置文件在哪里?
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Matlab / Simulink simulation of double closed loop DC speed regulation system