当前位置:网站首页>[C] PTA 6-8 finding the height of binary tree
[C] PTA 6-8 finding the height of binary tree
2022-07-29 04:29:00 【LastWhisperw】
This problem requires a given height of a binary tree .
Function interface definition :
int GetHeight( BinTree BT ); among BinTree The structure is defined as follows :
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
}; The function is required to return the given binary tree BT Height value of .
Ideas :
The height of a binary tree is the height of the root node .
The current node height is known = Maximum subtree height +1, So recursively find the left and right child nodes ( Left and right subtrees ) Height is good .
Time complexity :O(N) N It's the number of knots
Spatial complexity :O(1) Because only three variables are needed to record the height value
int GetHeight( BinTree BT ){
int hl=0;
int hr=0;
int h=0;
if(BT==NULL){
return 0;
}
hl=GetHeight(BT->Left);// Find the height of the left subtree
hr=(GetHeight(BT->Right));// Find the height of the right subtree
if(hl>hr){
h=hl+1;
}else{
h=hr+1;
}
return h;
} The following is the assistant who typed blindly because my article was too short :
Recursion is an easy way to understand .
So is there any other way to solve this problem besides recursion ?
The height of a tree can also be understood as the number of layers . We can try to use the idea of sequence traversal to calculate the height of the tree .
It would be nice if there was a variable in the node structure to record the level of the current node —— EH ,data No, just ? After all, this problem does not require the output of node data , therefore data You can tamper with it at will
Create and use a queue , The root node is assigned 1, Make it into the team ; The left and right children are assigned 2, The team ......
namely : Child node data value = Parent node data value +1, And enter the queue .
Set a int Variable h. Traverse the queue , If you compare h Give to h.
The team + The time complexity of leaving the team : O(N)
Spatial complexity :O(N)
...... Yes , Is negative optimization
So recursion is not always bad ( Stand hand )
边栏推荐
- 不会就坚持65天吧 只出现一次的数字
- C语言:浅谈各种复杂的声明
- Integration of Nan causes in pytorch training model
- Pix2.4.8 from start to installation (2021.4.4)
- 不会就坚持71天吧 链表排序
- There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
- Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
- TypeError: Cannot read properties of undefined (reading ‘then‘)
- 不会就坚持69天吧 合并区间
- leetcode 686.重复叠加字符串 KMP方法(C语言实现)
猜你喜欢

Vscode one click compilation and debugging

不会就坚持62天吧 单词之和

Back propagation process of manual BP neural network

It won't last for 65 days. It only appears once

Actual combat of flutter - DIO of request encapsulation (II)

15.federation

【Express连接MySQL数据库】

Flutter实战-请求封装(二)之dio

11.备份交换机

Definition and implementation of stack and queue (detailed)
随机推荐
10. Fallback message
C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan
Back propagation process of manual BP neural network
Not for 63 days. The biggest XOR
[common commands]
JVM (heap and stack) memory allocation
14. Haproxy+kept load balancing and high availability
Common components of solder pad (2021.4.6)
Semantic segmentation correlation
[express connection to MySQL database]
What is the use of meta-info?
Labelme cannot open the picture
Won't you insist on 71 days? List sorting
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
不会就坚持61天吧 最短的单词编码
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
Unity Foundation (3) -- various coordinate systems in unity