当前位置:网站首页>Basic operations on binary tree
Basic operations on binary tree
2022-06-24 09:50:00 【Herding】
List of articles
The second part of a binary tree

This part complete Something about binary trees Basic operation
// The former sequence traversal
void preOrderTraversal(Node root);
// In the sequence traversal
void inOrderTraversal(Node root);
// After the sequence traversal
void postOrderTraversal(Node root);
// Traversal ideas - Find the number of nodes
static int size = 0;
void getSize1(Node root);
// Sub problem ideas - Find the number of nodes
int getSize2(Node root);
// Traversal ideas - Find the number of leaf nodes
static int leafSize = 0;
void getLeafSize1(Node root);
// Sub problem ideas - Find the number of leaf nodes
int getLeafSize2(Node root);
// Sub problem ideas - Please k Number of layer nodes
int getKLevelSize(Node root);
// Get the height of the binary tree
int getHeight(Node root);
// lookup val Node , No return found null
// according to root -> The left subtree -> Search in the order of right subtree
// Once found , Return immediately , There is no need to continue looking elsewhere
Node find(Node root, int val);
Here, the previous, middle and subsequent iterations Above, Already completed .
1. Find the number of nodes
1. Traversal ideas
// Traversal ideas
int size = 0;
void getSize1(TreeNode root) {
if(root == null) {
return;
}
size++;
getSize1(root.left);
getSize1(root.right);
}

2. Sub problem ideas
// Sub problem ideas
int getSize2(TreeNode root) {
if(root == null) {
return 0;
}
return getSize2(root.left) + getSize2(root.right) + 1;
}

2. Find the number of leaf nodes
1. Traversal ideas
// Traversal ideas - Find the number of leaf nodes
int leafSize = 0;
void getLeafSize1(TreeNode root) {
if(root == null) {
return;
}
if(root.left == null && root.right == null) {
leafSize++;
}
getLeafSize1(root.left);
getLeafSize1(root.right);
}

2. Sub problem ideas
// Sub problem ideas - Find the number of leaf nodes
int getLeafSize2(TreeNode root) {
if(root == null) {
return 0;
}
if(root.left == null && root.right == null) {
// At present Of root by Leaf node
return 1;
}
return getLeafSize2(root.left) + getLeafSize2(root.right);
}

3 Please k Number of layer nodes
Here, let's look directly at the thinking of the sub problem
Sub problem ideas - Please k Number of layer nodes
// Sub problem ideas - Please k Number of layer nodes
int getKLevelSize(TreeNode root,int k) {
if(root == null || k < 0) {
return 0;
}
if(k == 1) {
return 1;
}
return getKLevelSize(root.left,k - 1) + getKLevelSize(root.right,k-1);
}

4. Get the height of the binary tree
int getHeight(TreeNode root) {
return root == null?0:Math.max(getHeight(root.left)+1,getHeight(root.right)+1);
}


I can't understand the above You can also watch this One Train of thought recurs down
int getHeight2(TreeNode root) {
if(root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
The maximum depth of a binary tree above oj topic

5. Find out if there is... In the binary tree val
TreeNode find(TreeNode root, char val) {
if(root == null) return null;
if(root.val == val) {
return root;
}
// TreeNode top = find(root.left,val);
// if(top != null) {
// return top;
// }
// TreeNode top2 = find(root.right,val);
// if(top2 != null ) {
// return top2;
// }
// return null;
// It can be abbreviated as
return find(root.right,val);
}
here Also do not have what difficulty It only needs Know two Boundary condition ok That's it root == null and root.val == val then Go to Recursive left subtree and Right subtree , Judge
root.val Is it equal to val that will do
Be careful here We use The method is The main function Zhongyao Use try and catch
because Nothing to look for val Returns the null here Use Null pointer exception occurs
try{
TreeNode treeNode1 = binaryTree.find(treeNode,'A');
System.out.println(treeNode1.val);
}catch(NullPointerException e) {
e.printStackTrace();
System.out.println(" Null pointer exception caught ");
}
6. Judge a lesson tree Whether it is a complete binary tree

boolean isCompleteTree(TreeNode root){
if(root == null) {
return true;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode top = queue.poll();
if(top != null) {
queue.offer(root.left);
queue.offer(root.right);
}else {
break;
}
while(!queue.isEmpty()) {
TreeNode top2 = queue.peek();
if(top != null) {
return false;
}
queue.poll();
}
}
return true;
}

边栏推荐
猜你喜欢

Why is LNX of e equal to X

文献调研报告

Learning Tai Chi Maker - esp8226 (XIII) OTA

英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!

ggplot2颜色设置总结

How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!

字节跳动-面试官: 谈下音视频同步原理,音频和视频能绝对同步吗?

tp5 使用post接收数组数据时报variable type error: array错误的解决方法

R ellipse random point generation and drawing

如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
随机推荐
PostgreSQL
Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
How to locate lock waiting in Dameng database
Ora-28000 error after upgrading Oracle 12C to 19C
Servlet fast foundation building
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
PhpStrom代码格式化设置
Implementation of simple floating frame in WindowManager
Servlet快速筑基
Go language project development practice directory
正则匹配手机号
impdp导schema报ORA-31625异常处理
Event registration Apache pulsar x kubesphere online meetup hot registration
Algorithm - the K row with the weakest combat power in the matrix (kotlin)
Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
【Eureka注册中心】
Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking
GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
Cdga | how can we do well in data governance?
Seekbar with text: customize progressdrawable/thumb: solve incomplete display