day21 二叉树的深度遍历的递归实现
1. 二叉树的遍历
- 前序遍历:根-左子树-右子树;
- 中序遍历:左子树-根-右子树;
- 后序遍历:左子树-右子树-根; 在实现二叉树的遍历时,使用递归把把复杂的问题转化为与原来问题相似的但规模较小的问题去解决。对于二叉树采用递归是好理解的,如果二叉树的深度高了 那递归不见得就是一个高效的算法。
2. 二叉树深度,结点数
对于一颗二叉树,用递归去求二叉树的深度和结点数,不管他有多少结点,我们在理解时都把他当成一个根结点,右边一个“右结点”,左边一个“左结点”,求深度我们只需要知道是一个根结点+(看左右结点是否存在来加1),求二叉树的结点数也是,只需要关注我这个结点左右结点是否有结点。
3. 代码
- 根据代码画出的图
- 代码
package main.java.datastructure.tree;
import java.time.OffsetDateTime;
public class BinaryCharTree {
/**
* The value in char
*/
char value;
/**
* The left child
*/
BinaryCharTree leftChild;
/**
* The right child
*/
BinaryCharTree rightChild;
/**
* The first constructor
* @param paraName
*/
public BinaryCharTree(char paraName){
value = paraName;
leftChild = null;
rightChild = null;
}
/**
* manually construct a tree.
*
* @return
*/
public static BinaryCharTree manualConstructTree(){
// Step 1. Construct a tree with only one node.
BinaryCharTree resultTree = new BinaryCharTree('a');
//Step 2. Construct all nodes. The first node is the root.
BinaryCharTree tempTreeB = new BinaryCharTree('b');
BinaryCharTree tempTreeC = new BinaryCharTree('c');
BinaryCharTree tempTreeD = new BinaryCharTree('d');
BinaryCharTree tempTreeE = new BinaryCharTree('e');
BinaryCharTree tempTreeF = new BinaryCharTree('f');
BinaryCharTree tempTreeG = new BinaryCharTree('g');
// Step 3. Link all nodes.
resultTree.leftChild = tempTreeB;
resultTree.rightChild = tempTreeC;
tempTreeB.rightChild = tempTreeD;
tempTreeC.leftChild = tempTreeE;
tempTreeD.leftChild = tempTreeF;
tempTreeD.rightChild = tempTreeG;
return resultTree;
}
/**
* pre-order visit
*/
public void preOrderVisit(){
System.out.print("" + value + " ");
if (leftChild != null){
leftChild.preOrderVisit();
}
if (rightChild != null){
rightChild.preOrderVisit();
}
}
/**
* in-order visit
*/
public void inOrderVisit(){
if (leftChild != null){
leftChild.inOrderVisit();
}
System.out.print("" + value + " ");
if (rightChild != null) {
rightChild.inOrderVisit();
}
}
/**
* Post-order visit.
*/
public void postOrderVisit(){
if (leftChild != null) {
leftChild.postOrderVisit();
}
if (rightChild != null) {
rightChild.postOrderVisit();
}
System.out.print("" + value + " ");
}
public int getDepth(){
// It is a leaf.
if ((leftChild == null) && (rightChild == null)) {
return 1;
}
// The depth of the left child.
int tempLeftDepth = 0;
if (leftChild != null){
tempLeftDepth = leftChild.getDepth();
}
int tempRighDepth = 0;
if (rightChild != null){
tempRighDepth = rightChild.getDepth();
}
if (tempLeftDepth >= tempRighDepth) {
return tempLeftDepth + 1;
}else {
return tempRighDepth + 1;
}
}
/**
* get the number of nodes
* @return
*/
public int getNumNodes(){
if (leftChild == null && rightChild == null){
return 1;
}
int tempLeftNodes = 0;
if (leftChild != null){
tempLeftNodes = leftChild.getNumNodes();
}
int tempRightNodes = 0;
if (rightChild != null) {
tempRightNodes = rightChild.getNumNodes();
}
return tempLeftNodes + tempRightNodes + 1;
}
public static void main(String args[]) {
BinaryCharTree tempTree = manualConstructTree();
System.out.println("\r\nPreorder visit:");
tempTree.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree.postOrderVisit();
System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
System.out.println("The number of nodes is: " + tempTree.getNumNodes());
}
}