定义TreeNode节点
package com.dhr.study.datastructure.binarytree;
public class TreeNode {
public Integer data;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode(Integer data){
this.data = data;
}
}
构建二叉树
public TreeNode createBinaryTree(LinkedList<Integer> inputList){
TreeNode node = null;
if(inputList == null || inputList.isEmpty()){
return null;
}
Integer data = inputList.removeFirst();
if(data != null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild = createBinaryTree(inputList);
}
return node;
}
public static void main(String[] args) {
LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(3, 2, 9, null, null, 10, null, null, 8, null, 4));
TestTreeNode testTreeNode = new TestTreeNode();
TreeNode binaryTree = testTreeNode.createBinaryTree(inputList);
}
二叉树 前序遍历
public void preOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
System.out.println(treeNode.data);
preOrderTraversal(treeNode.leftChild);
preOrderTraversal(treeNode.rightChild);
}
二叉树 中序遍历
public void inOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
inOrderTraversal(treeNode.leftChild);
System.out.println(treeNode.data);
inOrderTraversal(treeNode.rightChild);
}
二叉树 后序遍历
public void postOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
postOrderTraversal(treeNode.leftChild);
postOrderTraversal(treeNode.rightChild);
System.out.println(treeNode.data);
}
完整代码
package com.dhr.study.datastructure.binarytree;
import java.util.Arrays;
import java.util.LinkedList;
public class TestTreeNode {
public static void main(String[] args) {
LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(3, 2, 9, null, null, 10, null, null, 8, null, 4));
TestTreeNode testTreeNode = new TestTreeNode();
TreeNode binaryTree = testTreeNode.createBinaryTree(inputList);
System.out.println("前序遍历");
testTreeNode.preOrderTraversal(binaryTree);
System.out.println("中序遍历");
testTreeNode.inOrderTraversal(binaryTree);
System.out.println("后序遍历");
testTreeNode.postOrderTraversal(binaryTree);
}
public TreeNode createBinaryTree(LinkedList<Integer> inputList){
TreeNode node = null;
if(inputList == null || inputList.isEmpty()){
return null;
}
Integer data = inputList.removeFirst();
if(data != null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild = createBinaryTree(inputList);
}
return node;
}
public void preOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
System.out.println(treeNode.data);
preOrderTraversal(treeNode.leftChild);
preOrderTraversal(treeNode.rightChild);
}
public void inOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
inOrderTraversal(treeNode.leftChild);
System.out.println(treeNode.data);
inOrderTraversal(treeNode.rightChild);
}
public void postOrderTraversal(TreeNode treeNode){
if(treeNode == null){
return;
}
postOrderTraversal(treeNode.leftChild);
postOrderTraversal(treeNode.rightChild);
System.out.println(treeNode.data);
}
}