当前位置:网站首页>Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
2022-06-13 01:21:00 【Torlesse】
Tree traversal - The first sequence traversal 、 In the sequence traversal 、 After the sequence traversal
The first sequence traversal
Binary Tree Preorder Traversal , Is to output the root node first , Then traverse the left subtree , Finally, traverse the right subtree , When traversing the left subtree and the right subtree , Also follow the rules of preorder traversal .
/** * The first sequence traversal * @param treeNode */
public void preOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
System.out.print(" " + treeNode.value);
preOrder(treeNode.left);
preOrder(treeNode.right);
}
In the sequence traversal
Middle order traversal of binary trees , Is to recursively traverse the left subtree in the middle order first , Then output the value of the root node , Then traversing the right subtree in recursive middle order .
/** * In the sequence traversal * @param treeNode */
public void inOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
inOrder(treeNode.left);
System.out.print(" " + treeNode.value);
inOrder(treeNode.right);
}
After the sequence traversal
Postorder traversal of binary trees , It is to recurse first and then traverse the left subtree , Then recursively traverse the right subtree , Finally, output the value of the root node .
/** * After the sequence traversal * @param treeNode */
public void postOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
postOrder(treeNode.left);
postOrder(treeNode.right);
System.out.print(" " + treeNode.value);
}
Case study :
public class TreeDemo {
public class TreeNode{
private Integer value;
private TreeNode right;
private TreeNode left;
public TreeNode() {
}
public TreeNode(Integer value, TreeNode left, TreeNode right) {
this.value = value;
this.right = right;
this.left = left;
}
public Integer getvalue() {
return value;
}
public void setvalue(Integer value) {
this.value = value;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
}
public static void main(String[] args) {
TreeDemo treeDemo = new TreeDemo();
TreeNode tree = treeDemo.getTree();
System.out.println(" The first sequence traversal :");
treeDemo.preOrder(tree);
System.out.println();
System.out.println(" In the sequence traversal :");
treeDemo.inOrder(tree);
System.out.println();
System.out.println(" After the sequence traversal :");
treeDemo.postOrder(tree);
}
public TreeNode getTree(){
TreeNode treeNode = new TreeNode();
treeNode.setvalue(13);
treeNode.setLeft(new TreeNode(10,
new TreeNode(7,
new TreeNode(3,null,null),new TreeNode(4,null,null)),
new TreeNode(12,new TreeNode(11,null,null),null)));
treeNode.setRight(new TreeNode(15,
new TreeNode(14,null,null),
new TreeNode(17,null,null)));
return treeNode;
}
/** * The first sequence traversal * @param treeNode */
public void preOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
System.out.print(" " + treeNode.value);
preOrder(treeNode.left);
preOrder(treeNode.right);
}
/** * In the sequence traversal * @param treeNode */
public void inOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
inOrder(treeNode.left);
System.out.print(" " + treeNode.value);
inOrder(treeNode.right);
}
/** * After the sequence traversal * @param treeNode */
public void postOrder(TreeNode treeNode){
if(treeNode == null){
return;
}
postOrder(treeNode.left);
postOrder(treeNode.right);
System.out.print(" " + treeNode.value);
}
}
give the result as follows :
边栏推荐
- Detailed explanation of Joseph problem
- RSA encryption colloquial explanation
- How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
- Realization of flip animation
- Get preview of precast body
- Facial expression recognition dataset
- Set and array conversion, list, array
- Several categories of software testing are clear at a glance
- Vector|hdu-4841 round table questions
- Loss calculation in pytorch
猜你喜欢

軟件測試的幾種分類,一看就明了

Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe

MySQL related summary

MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large(4223215 > 4194304)

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of vrsi, bbiboll, WR, bias and RSI indicators

Matrix fast power

Five classic articles worth reading (2)

工作与生活

How to print infinite symbol in WPS
随机推荐
Simple operation of MySQL database
Page optimization - Notes
Et5.0 configuring Excel
304. Merge two ordered arrays
软件测试的几种分类,一看就明了
FSOs forest simulation optimization model learning notes
Unity calls alertdialog
Alexnet实现Caltech101数据集图像分类(pytorch实现)
4K sea bottom and water surface fabrication method and ocean bump displacement texture Download
Database query user mailbox
spiral matrix visit Search a 2D Matrix
使用Pygame创建一个简单游戏界面
Go JWT learning summary
HashSet underlying source code
Pysmb usage
Leetcode question brushing 04 string
项目实训(十七)---个人工作总结
Argparse command line passes list type parameter
Higherhrnet pre training model -- download from network disk
707. design linked list