当前位置:网站首页>[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 )
边栏推荐
猜你喜欢
![[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)](/img/fe/8c707c30c734de7bb76ea68134842c.png)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)

VScode 一键编译和调试

Not for 58 days. Implement prefix tree

9.延迟队列

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

Common components of solder pad (2021.4.6)

Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2

Definition and implementation of stack and queue (detailed)

Make a virtual human with zego avatar | virtual anchor live broadcast solution

不会就坚持64天吧 查找插入位置
随机推荐
Machine vision series 3:vs2019 opencv environment configuration
不会就坚持71天吧 链表排序
Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
不会就坚持61天吧 最短的单词编码
[c language] PTA 7-48 find the number of combinations
不会就坚持70天吧 数组中第k大的数
Not for 60 days, magical dictionary
C language: structure simple syntax summary
Pyqt5 learning pit encounter and pit drainage (3) background picture coverage button style and check button status
不会就坚持65天吧 只出现一次的数字
Mongo Shell交互式命令窗口
Definition and implementation of stack and queue (detailed)
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
Vscode one click compilation and debugging
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
C language: talking about various complex statements
C language: typedef knowledge points summary
JVM (heap and stack) memory allocation
The daily life of programmers
Semantic segmentation correlation