日撸Java三百行(day25:栈实现二叉树深度遍历之中序遍历)

news2024/9/22 17:41:18

目录

一、栈实现二叉树遍历的可行性

二、由递归推出栈如何实现中序遍历

1.左子树入栈

2.根结点出栈

3.右子树入栈

4.实例说明

三、代码实现

总结


 

一、栈实现二叉树遍历的可行性

在日撸Java三百行(day16:递归)中,我们讲过“递归”说白了就是函数自身调用自身,当递归函数调用自身时,我们可以看作是入栈的过程,调用一次就入栈一次;而当递归满足结束条件后,一层一层返回时,又可以看作是出栈的过程,返回一层就出栈一次。这样看来,递归实现和栈实现似乎是可以相互转化的,毕竟它们都满足“先进后出、后进先出”的原则。

在日撸Java三百行(day21:二叉树的深度遍历的递归实现)中,我们是利用递归的方法对二叉树进行的前中后序遍历,那么既然递归实现和栈实现可以相互转化,我们是否可以利用栈的思想来完成二叉树的遍历呢?今天我们就先从用栈实现二叉树的中序遍历开始(因为中序遍历是几种遍历中最简单的)。

二、由递归推出栈如何实现中序遍历

先来回顾一下我们之前是怎么用递归实现二叉树中序遍历的,代码如下:

    /**
	 *********************
	 * In-order visit.
	 *********************
	 */
	public void inOrderVisit() {
		if(leftChild != null) {
			leftChild.inOrderVisit();
		} // of if
		
		System.out.print("" + value + " ");
		
		if(rightChild != null) {
			rightChild.inOrderVisit();
		} // of if
	} // of inOrderVisit

中序遍历是按“左子树 根结点 右子树”的顺序进行遍历,上述的递归函数就是先判断当前根结点的左子树是否为空,不空则搁置当前这层函数操作,将该左子树作为新的根结点,再次调用函数;空则直接输出当前根结点,再按照同样的方法判断右子树。这个过程如果用栈来完成,可以简单概括为三步,即左子树入栈、根结点出栈、右子树入栈,下面我们就来对这三步进行分析说明。

1.左子树入栈

由上边递归函数的代码顺序可知,每次调用函数时都会优先进入左子树,如果当前左子树不空,就会进入新一层的递归函数;进入新一层的函数后,再次优先进入左子树,如果该左子树仍不空,则再次进入下一层递归函数……总结一下就是,如果左子树持续不空,那么就会一直朝着左子树的方向行进,直到某个结点的左子树为空。为了便于理解,我们以下图为例:

  • 首先a作为根结点调用递归函数
  • 进入a的左子树b
  • a的左子树b不为空,于是将b作为新的根结点调用递归函数
  • 进入b的左子树d
  • b的左子树d不为空,于是将d作为新的根结点调用递归函数
  • 进入d的左子树h
  • d的左子树h不为空,于是将h作为新的根结点调用递归函数
  • h没有左子树,停止调用

a821063aa4c74d4c807bb5327fa911a8.png

我们在一开始说过,调用一次递归函数就可以看作是入栈一次,所以上述过程,如果用栈来实现, 就是依次入栈左子树,如果该左子树中还有左子树,则继续入栈左子树,直到某个结点的左子树为空,其实也就是按照上图中红色箭头的方向持续入栈左子树。不过需要注意,每个根结点都必须在其左子树之前入栈,上图的二叉树入栈后结果如下:

c40e507e2aeb443b9d39c8ebf2de8fb1.png

2.根结点出栈

根据上面递归函数的代码,可以知道由于上图中h结点的左子树为空,所以不会继续调用函数,而是来到第11行代码直接输出h结点。这个过程反映到栈中,就是将此时的栈顶元素——h结点出栈并访问它。

3.右子树入栈

在上述的递归函数中,输出结点h后,接下来我们就要开始判断其右子树了,如果其右子树不为空,那么就把它的右子树作为新的根结点调用递归函数。用栈的思想考虑,就是如果此时出栈元素的右子树不空,就将它的右子树入栈,然后再从该右子树出发(即把该右子树当作当前根结点),按照“左子树入栈、根结点出栈、右子树入栈”的顺序进行;而如果此时出栈元素的右子树为空,则将当前栈顶元素进行出栈,然后继续判断新出栈元素的右子树。

4.实例说明

我们简单总结一下以上三步,先将左子树依次入栈,然后出栈当前栈顶元素,再判断该出栈元素的右子树,最后根据判断结果执行。

栈实现二叉树遍历用文字语言叙述,真的既拗口又不好理解,下面我们还是用一个具体的例子来说明,这样稍微直观一点。对于下图的二叉树,栈实现中序遍历的具体步骤如下:

89d9fc64d939465d9867d8f4473ac039.png

  • 左子树依次入栈。放在这里就是将a、b、d依次入栈,由于d之后没有左子树了,所以入栈到d这里就暂时停止了。
  • 将当前栈顶元素d出栈,并输出d。
  • 判断此时出栈元素的右子树。由于此时出栈元素d的右子树为空,所以下一步应该将新的栈顶元素出栈。
  • 将新的栈顶元素b出栈,并输出b。
  • 判断此时出栈元素的右子树。由于此时出栈元素b的右子树不空,所以下一步应该将它的右子树入栈。
  • 将b的右子树e入栈。(一旦右子树入栈,下一步就是该右子树的左子树依次入栈)
  • 左子树再次依次入栈。放到这里就是将e之后的左子树依次入栈,由于h之后没有左子树了,所以入栈到h这里就暂时停止了。
  • 将当前栈顶元素h出栈,并输出h。
  • 判断此时出栈元素的右子树。由于此时出栈元素h的右子树为空,所以下一步应该将新的栈顶元素出栈。
  • 将新的栈顶元素e出栈,并输出e。
  • 判断此时出栈元素的右子树。由于此时出栈元素e的右子树为空,所以下一步应该将新的栈顶元素出栈。
  • 将新的栈顶元素a出栈,并输出a。
  • 判断此时出栈元素的右子树。由于此时出栈元素a的右子树不空,所以下一步应该将它的右子树入栈。
  • 将a的右子树c入栈。(一旦右子树入栈,下一步就是该右子树的左子树依次入栈)
  • 左子树再次依次入栈。放到这里就是将c之后的左子树依次入栈,由于f之后没有左子树了,所以入栈到f这里就暂时停止了。
  • 将当前栈顶元素f出栈,并输出f。
  • 判断此时出栈元素的右子树。由于此时出栈元素f的右子树不空,所以下一步应该将它的右子树入栈。
  • 将f的右子树i入栈。(一旦右子树入栈,下一步就是该右子树的左子树依次入栈)
  • 左子树再次依次入栈。但是由于此时i没有左子树了,所以这一步跳过。
  • 将当前栈顶元素i出栈,并输出i。
  • 判断此时出栈元素的右子树。由于此时出栈元素i的右子树为空,所以下一步应该将新的栈顶元素出栈。
  • 将新的栈顶元素c出栈,并输出c。
  • 判断此时出栈元素的右子树。由于此时出栈元素c的右子树不空,所以下一步应该将它的右子树入栈。
  • 将c的右子树g入栈。(一旦右子树入栈,下一步就是该右子树的左子树依次入栈)
  • 左子树再次依次入栈。但是由于此时g没有左子树了,所以这一步跳过。
  • 将当前栈顶元素g出栈,并输出g。
  • 判断此时出栈元素的右子树。由于此时出栈元素g的右子树为空,所以下一步应该将新的栈顶元素出栈。
  • 将新的栈顶元素出栈。但是由于此时栈已空同时当前结点也为空,所以到此就完成了。

76ec570bc44348d7a55af52dd171dac2.png

178345ff25fd4b2e8bb3c0677c8c366d.png 

由这个例子,我们可以得出以下便于后续编程的结论:

  • 出栈后立马输出该出栈元素
  • 当栈空且结点也为空时,遍历结束

三、代码实现

大概理解这个过程之后,我们还是先开始代码模拟吧(毕竟用文字解释感觉真的不好说清楚,不过也有可能是我的语言表达水平有限吧…)

为了提高代码的复用性,这里我们重写了一个和通用性队列类似的通用性栈,代码如下:

package datastructure;

/**
 *Object stack.
 *
 *@auther Xin Lin 3101540094@qq.com.
 */

public class ObjectStack {
	
	/**
	 * The depth.
	 */
	public static final int MAX_DEPTH = 10;
	
	/**
	 * The actual depth.
	 */
	int depth;
	
	/**
	 * The data.
	 */
	Object[] data;

	/**
	 *********************
	 * Construct an empty Object stack.
	 *********************
	 */
	public ObjectStack() {
		depth = 0;
		data = new Object[MAX_DEPTH];
	} // Of the first constructor
	
	/**
	 *********************
	 * Overrides the method claimed in Object, the superclass of any class.
	 *********************
	 */
	public String toString() {
		String resultString = "";
		
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		} // Of for i
		
		return resultString;
	} // Of toString
	
	/**
	 *********************
	 * Push an element.
	 * 
	 * @param paraObject The given object.
	 * @return Success or not.
	 *********************
	 */
	public boolean push(Object paraObject) {
		if (depth == MAX_DEPTH) {
			System.out.println("Stack full.");
			return false;
		} // Of if
		
		data[depth] = paraObject;
		depth++;
		
		return true;
	} // Of push
	
	/**
	 *********************
	 * Pop an element.
	 * 
	 * @return The object at the top of the stack.
	 *********************
	 */
	public Object pop() {
		if(depth == 0) {
			System.out.println("Nothing to pop.");
			return '\0';
		} // Of if
		
		Object resultObject = data[depth - 1];
		depth--;
		
		return resultObject;
	} // Of pop
	
	/**
	 *********************
	 * Is the stack empty?
	 * 
	 * @return True if empty.
	 *********************
	 */
	public boolean isEmpty() {
		if(depth == 0) {
			return true;
		} // Of if
		
		return false;
	} // Of isEmpty
	
	/**
	 *********************
	 *The entrance of the program.
	 *
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String[] args) {
		ObjectStack tempStack = new ObjectStack();
		
		for(char ch = 'a'; ch < 'm'; ch++) {
			tempStack.push(new Character(ch));
			System.out.println("The current stack is: " + tempStack);
		} // Of for ch
		
		char tempChar;
		for(int i = 0; i < 12; i++) {
			tempChar = ((Character)tempStack.pop()).charValue();
			System.out.println("Popped: " + tempChar);
			System.out.println("The current stack is: " + tempStack);
		} // Of for i
	} // Of main
} // Of class ObjectStack

在重写的过程中,一定要注意强制类型转换的使用。比如倒数第6行代码中,由于栈是Object类型的栈,所以得到的出栈元素肯定也是Object类型,因此我们需要使用Character先将其强制转换成Character类型,再利用charValue()方法将Character类型转换为基本数据类型char,最后再赋给同为char类型的变量tempChar。

现在我们开始创建方法,首先创建一个ObjectStack类型的对象栈,以及一个二叉树的结点引用;然后,定义一个while循环,循环条件为栈不空或者结点不空(因为栈空且结点空的时候,遍历就结束了)。

在while循环中,如果当前结点不空,则将其入栈,再利用tempNode = tempNode.leftChild不断往下迭代左子树,具体来说就是不断地将当前结点的左子树作为新的当前结点;如果当前结点为空,则说明当前结点的根结点没有左子树,所以根据中序遍历“左 根 右”的顺序要求,此时就直接输出当前结点的根结点,也就是输出此时的栈顶元素(注意先出栈再打印);然后,将该出栈元素的右子树作为新的当前结点,继续判断。

    /**
	 *********************
	 * In-order visit with stack.
	 *********************
	 */
	public void inOrderVisitWithStack() {
		ObjectStack tempStack = new ObjectStack();
		BinaryCharTree tempNode = this;
		while(!tempStack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				tempStack.push(tempNode);
				tempNode = tempNode.leftChild;
			} else {
				tempNode = (BinaryCharTree)tempStack.pop();
				System.out.print("" + tempNode.value + " ");
				tempNode = tempNode.rightChild;
			} // Of if
		} // Of while
	} // Of inOrderVisitWithStack

最后,我们用昨天创建的二叉树tempTree2来进行数据测试,如下:

System.out.println("\r\nIn-order visit with stack: ");
tempTree2.inOrderVisitWithStack();

完整的程序代码:

package datastructure.tree;

import datastructure.*;
import java.util.Arrays;
/**
 * Binary tree with char type elements.
 *
 *@auther Xin Lin 3101540094@qq.com.
 */

public class BinaryCharTree {

	/**
	 * The value
	 */
	char value;
	
	/**
	 * The left child
	 */
	BinaryCharTree leftChild;
	
	/**
	 * The right child
	 */
	BinaryCharTree rightChild;
	
	/**
	 *********************
	 * The first constructor.
	 * 
	 * @param paraName The value.
	 *********************
	 */
	public BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	} // Of constructor
	
	/**
	 *********************
	 * Manually construct a tree. Only for testing.
	 *********************
	 */
	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 tempTreeA = resultTree.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;
	} // Of manualConstructTree
	
	/**
	 *********************
	 * Pre-order visit.
	 *********************
	 */
	public void preOrderVisit() {
		System.out.print("" + value + " ");
		
		if(leftChild != null) {
			leftChild.preOrderVisit();
		} // Of if
		
		if(rightChild != null) {
			rightChild.preOrderVisit();
		} // Of if
	} // Of preOrderVisit
	
	/**
	 *********************
	 * In-order visit.
	 *********************
	 */
	public void inOrderVisit() {
		if(leftChild != null) {
			leftChild.inOrderVisit();
		} // Of if
		
		System.out.print("" + value + " ");
		
		if(rightChild != null) {
			rightChild.inOrderVisit();
		} // Of if
	} // Of inOrderVisit
	
	/**
	 *********************
	 * Post-order visit.
	 *********************
	 */
	public void postOrderVisit() {
		if(leftChild != null) {
			leftChild.postOrderVisit();
		} // Of if
		
		if(rightChild != null) {
			rightChild.postOrderVisit();
		} // Of if
		
		System.out.print("" + value + " ");
	} // Of postOrderVisit
	
	/**
	 *********************
	 * Get the depth of the binary char tree.
	 * 
	 * @return The depth.
	 *********************
	 */
	public int getDepth() {
		if((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if
		
		// The depth of the left child.
		int tempLeftDepth = 0;
		if(leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		} // Of if
		
		// The depth of the right child.
		int tempRightDepth = 0;
		if(rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		} // Of if
		
		if(tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		} // Of if
	} // Of getDepth
	
	/**
	 *********************
	 * Get the number of nodes of the binary char tree.
	 * 
	 * @return The number of nodes.
	 *********************
	 */
	public int getNumNodes() {
		if((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if
		
		// The number of nodes of the left child.
		int tempLeftNodes = 0;
		if(leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		} // Of if
		
		// The number of nodes of the right child.
		int tempRightNodes = 0;
		if(rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		} // Of if
		
		// The total number of nodes.
		return tempLeftNodes + tempRightNodes + 1;
	} // Of getNumNodes
	
	/**
	 * The values of nodes according to breadth first traversal.
	 */
	char[] valuesArray;

	/**
	 * The indices in the complete binary tree.
	 */
	int[] indicesArray;

	/**
	 ********************
	 * Convert the tree to data arrays, including a char array and an int array.
	 * The results are stored in two member variables.
	 * 
	 * @see #valuesArray
	 * @see #indicesArray
	 *********************
	 */
	public void toDataArrays() {
		//Initialize arrays.
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;

		//Traverse and convert at the same time.
		CircleObjectQueue tempQueue = new CircleObjectQueue();
		tempQueue.enqueue(this);
		CircleIntQueue tempIntQueue = new CircleIntQueue();
		tempIntQueue.enqueue(0);

		BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
		int tempIndex = tempIntQueue.dequeue();
		while (tempTree != null) {
			valuesArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;

			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(tempIndex * 2 + 1);
			} // Of if

			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIntQueue.enqueue(tempIndex * 2 + 2);
			} // Of i
			tempTree = (BinaryCharTree) tempQueue.dequeue();
			tempIndex = tempIntQueue.dequeue();
		} // Of while
	} // Of toDataArrays
	
	/**
	 ********************
	 * Convert the tree to data arrays, including a char array and an int array.
	 * The results are stored in two member variables.
	 * 
	 * @see #valuesArray
	 * @see #indicesArray
	 *********************
	 */
	public void toDataArraysObjectQueue() {
		//Initialize arrays.
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;

		//Traverse and convert at the same time.
		CircleObjectQueue tempQueue = new CircleObjectQueue();
		tempQueue.enqueue(this);
		CircleObjectQueue tempIntQueue = new CircleObjectQueue();
		Integer tempIndexInteger = Integer.valueOf(0);
		tempIntQueue.enqueue(tempIndexInteger);
		
		BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
		int tempIndex = ((Integer)tempIntQueue.dequeue()).intValue();
		System.out.println("tempIndex = " + tempIndex);
		while (tempTree != null) {
			valuesArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;
			
			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 1));
			} // Of if
			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 2));
			} // Of if
			
			tempTree = (BinaryCharTree) tempQueue.dequeue();
			if (tempTree == null) {
			     break;
			} // Of if
			
			tempIndex = ((Integer)tempIntQueue.dequeue()).intValue();
		} // Of while
	} // Of toDataArraysObjectQueue

	/**
	 *********************
	 * The second constructor. The parameters must be correct since no validity
	 * check is undertaken.
	 * 
	 * @param paraDataArray    The array for data.
	 * @param paraIndicesArray The array for indices.
	 *********************
	 */
	public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
		// Step 1. Use a sequential list to store all nodes.
		int tempNumNodes = paraDataArray.length;
		BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
		for(int i = 0; i < tempNumNodes; i++) {
			tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
		} // Of for i
		
		// Step 2. Link all nodes.
		for(int i = 1; i < tempNumNodes; i++) {
			for(int j = 0; j < i; j++) {
				System.out.println("Indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
				if(paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
					tempAllNodes[j].leftChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				} // Of if
				if(paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
					tempAllNodes[j].rightChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				} // Of if
			} // Of for j
		} // Of for i
		
		// Step 3. The root is the first node.
		value = tempAllNodes[0].value;
		leftChild = tempAllNodes[0].leftChild;
		rightChild = tempAllNodes[0].rightChild;
	} // Of the the second constructor
	
	/**
	 *********************
	 * In-order visit with stack.
	 *********************
	 */
	public void inOrderVisitWithStack() {
		ObjectStack tempStack = new ObjectStack();
		BinaryCharTree tempNode = this;
		while(!tempStack.isEmpty() || tempNode != null) {
			if(tempNode != null) {
				tempStack.push(tempNode);
				tempNode = tempNode.leftChild;
			} else {
				tempNode = (BinaryCharTree)tempStack.pop();
				System.out.print("" + tempNode.value + " ");
				tempNode = tempNode.rightChild;
			} // Of if
		} // Of while
	} // Of inOrderVisitWithStack
	
	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	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());

		tempTree.toDataArrays();
		System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
		
		tempTree.toDataArraysObjectQueue();
		System.out.println("Only object queue.");
		System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
		
		char[] tempCharArray = {'A', 'B', 'C', 'D', 'E', 'F'};
		int[] tempIndices = {0, 1, 2, 4, 5, 12};
		BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndices);
		
		System.out.println("\r\nPreorder visit:");
		tempTree2.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree2.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree2.postOrderVisit();
		
		System.out.println("\r\nIn-order visit with stack: ");
		tempTree2.inOrderVisitWithStack();
	}// Of main	
} // Of class BinaryCharTree

运行结果:

c366944c3eff427181d89920512e740d.png

可以发现,对于同一棵二叉树tempTree2,我们今天用栈实现的中序遍历和我们昨天用递归实现的中序遍历,其结果是一模一样的,说明代码可行。

总结

今天,我们主要学习的就是如何利用栈来实现二叉树的中序遍历,其本质上就是递归思维和迭代思维的相互转化。单看今天的代码量的话,其实挺少的,但是如果想要说清楚理透彻这两种思维的转化过程,似乎就比较困难了,本文也只是作者个人一些浅薄的理解,如有误,欢迎批评指正。

通过今天的学习,我们可以发现果然还是递归用起来简单,不过我们还是需要像二叉树遍历这种较为复杂的迭代操作,这对于锻炼一个程序员的迭代思维还是非常好的。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2047004.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Debian 12 基于KubeAdm搭建多节点K8S 1.28.x集群

背景 CentOS 7 官方支持和更新已于2024年6月30日结束。这意味着CentOS 7 不再接受官方的更新和补丁。并且官方推荐用户迁移到新的操作系统&#xff1b;而转移到Debian的优势有&#xff1a; 更加成熟的软件包管理系统&#xff1a;Debian 的包管理系统是 APT&#xff08;Advanc…

cleanmymacx官网2024中文官方地址

大家好&#xff0c;我是你们的科技小助手。今天来跟大家聊聊一款非常神奇的产品——cleanmymacx。这是一款Mac专用的系统优化工具&#xff0c;最近刚刚更新了新功能哦&#xff01;你们是不是经常觉得电脑越来越慢&#xff0c;硬盘空间不足&#xff0c;但又不知道该删除什么文件…

机器学习系列—深入探索弗里德曼检验:非参数统计分析的利器

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

滴滴开源新项目Unify:聚焦Flutter与原生通信难题,助力跨端应用落地

引言 在移动开发领域&#xff0c;移动跨端技术因其提效收益&#xff0c;逐渐成为业界趋势之一。Flutter 作为近年来热门的跨端技术&#xff0c;以高性能、自渲染、泛跨端著称&#xff0c;得到广泛应用。在滴滴国际化业务中&#xff0c;我们大量应用 Flutter。目前已在滴滴国际化…

Sentinel集成Apollo持久化配置的技术方案

作者本人&#xff0c;简放视野 https://github.com/bert82503 背景 Sentinel 介绍 Sentinel 是流量治理组件&#xff0c;主要以流量为切入点&#xff0c;从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性…

拥有一个公网固定IP,既然如此简单、HTTP 虚拟专线:为您开启专属网络访问新时代

#HTTP 虚拟专线# 网络的便捷性和稳定性对于个人和企业来说至关重要。我们自豪地为您介绍一款具有创新性的产品 ——HTTP 虚拟专线&#xff0c;它将为您的网络体验带来前所未有的改变。 每位 VHTTP 用户都将分配一个专属的固定公网 IP 地址&#xff0c;这意味着您不再需要担心 I…

【视频监控】通过TCP协议搭建客户端和服务端使用OpenCV实现Linux开发板摄像头图像实时回传本地

一. 前言 本文主要实现了远程开发板摄像头画面实时传输回本地电脑进而达到视频监控功能。主要分为开发板客户端和电脑服务端的两部分代码讲解。 本文使用的是米尔的Remi Pi开发板&#xff0c;摄像头是米尔配套的MY-CAM003M&#xff0c;开发板Python环境为3.8&#xff0c;电脑…

Java的jdk配置成功,但是输入java -version等,命令行没有任何反应

问题 Java下载后&#xff0c;手动配置环境变量&#xff0c;并且配置好&#xff0c;但是在命令行中无论输入java的什么都没有反应 解决方案 将手动配置的环境变量放到最前面 重新尝试 java -version命令

C++模板的特化

目录 一、模板特化概念 二、函数模板特化 三、类模板特化 1.全特化 2.偏特化 3.总结 4.类模板特化实例应用 一、模板特化概念 函数模板和类模板都有特化&#xff1a; 通常情况下使用模板可以实现一些与类型无关的代码&#xff0c;但一些特殊类型可能会出错&#xff0c;…

报错:Can‘t find Python executable “python“, you can set the PYTHON env variable

将项目导入vscode,执行npm install命令后&#xff0c;报错了&#xff0c;报错的信息是node-sass安装失败&#xff0c;同时提示需要python环境的错误信息&#xff0c;这是因为安装node-sass失败了&#xff0c;而node-sass依赖于Python环境。 1.报错&#xff1a;Cant find Python…

基于Hadoop的微博社交媒体用户大数据分析【海量数据】

文章目录 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主项目介绍数据集展示Hadoop脚本文件可视化展示每文一语 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主 项目介绍 本项目基于hadoop的社交媒体用户进行大数据…

把照片制作成精美的仿真电子相册

​在这个数字化时代&#xff0c;我们拍摄的照片越来越多&#xff0c;但往往缺乏一个合适的方式来展示和保存这些珍贵的回忆。今天&#xff0c;我将向大家介绍如何将你的照片制作成一个精美的仿真电子相册&#xff0c;让你的回忆更加生动和持久。 第一步&#xff1a;选择合适的照…

FreeRTOS 3

一&#xff0c;信号量 有时候任务之间传递的只是一个标致&#xff0c;让进程之间同步&#xff0c;会对一个共享资源的互斥性访问&#xff0c;这时候就可以用信号量和互斥量。 1&#xff0c;二值信号量 2&#xff0c;计数信号量 3&#xff0c;互斥量 3.1&#xff0c;差别 4&…

Orangepi 5 Pro(香橙派5pro)部署yolov5

前言 香橙派内置了6T算力的NPU&#xff0c;想着可以跑一下yolov5&#xff0c;看看香橙派的速度如何。 在开始部署之前&#xff0c;需要具备一定的linux技能——vim、linux常见指令、conda等等。如果没有这些技能的话&#xff0c;做下去会有一定的难度&#xff0c;可以先看几遍了…

19.实现一个算法实现删除链表中倒数第 n 个结点

19. Remove Nth Node From End of List 题目 Given the head of a linked list, remove the nth node from the end of the list and return its head. Follow up: Could you do this in one pass? Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]Example…

蚂蚁AL1 15.6T 创新科技的新典范

● 哈希率&#xff1a;算力达到15.6T&#xff08;相当于15600G&#xff09;&#xff0c;即每秒能够进行15.6万亿次哈希计算&#xff0c;在同类产品中算力较为出色&#xff0c;能提高WA掘效率。 ● 功耗&#xff1a;功耗为3510W&#xff0c;虽然数值看似不低&#xff0c;但结合其…

PythonStudio 控件使用常用方式(二十七)TActionList

PythonStudio是一个极强的开发Python的IDE工具&#xff0c;官网地址是&#xff1a;https://glsite.com/ &#xff0c;在官网可以下载最新版的PythonStudio&#xff0c;同时&#xff0c;在使用PythonStudio时&#xff0c;它也能及时为用户升到最新版本。它使用的是Delphi的控件&…

Python爬虫技术与K-means算法的计算机类招聘信息获取与数据分析

有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主 目录 摘要.... 1 Abstract 2 1 引言.... 3 1.1 研究背景... 3 1.2 国内外研究现状... 4 1.3 研究目的... 5 1.4 研究意义... 7 2 关键技术理论介绍... 7 2.1 Python爬虫... 7 2.1 K-means…

消灭星星游戏程序设计【连载十一】——在线程中解决音效卡顿问题

消灭星星游戏程序设计【连载十一】——在线程中解决音效卡顿问题 大家每次都可以在页面中下载本节内容的实现代码&#xff0c;一步一步从简单开始&#xff0c;逐步完成游戏的各种功能&#xff0c;如果大家有任何问题也欢迎留言交流。 游戏整体效果展示&#xff1a; 1、本节要达…

宠物空气净化器哪款好?希喂、有哈宠物空气净化器测评

回想起几年前那个午后&#xff0c;我意外的在路边捡到了两只小猫咪&#xff0c;心中莫名有一份责任感出现&#xff0c;所以没有丝毫犹豫我就决定将它们带回家。捡回家以后&#xff0c;家里确实多了几分温馨&#xff0c;逐渐成为我的精神支柱。小猫的到来&#xff0c;让家的每一…