二叉树

2022/5/4 23:18:36

本文主要是介绍二叉树,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

------------恢复内容开始------------

递归序 每个节点都会 来到 三次 然后根据在每次来的时候,在哪一次操作,分为三种遍历,都是基于根节点为参考 - 先序, 头 左 右 - 中序, 左 头 右 - 后序, 左 右 头 递归实现 ```java public static class Node { int value; Node left; Node right; public Node(int value) { this.value = value; } } public static void preOrderRecur(Node head) { if (head == null) { return; } // 1 System.out.println(head.value + " "); preOrderRecur(head.left); // 2 preOrderRecur(head.right); // 3 } public static void inOrderRecur(Node head) { if (head == null) { return; } // 1 inOrderRecur(head.left); // 2 System.out.println(head.value + " "); inOrderRecur(head.right); // 3 } public static void postOrderRecur(Node head) { if (head == null) { return; } // 1 postOrderRecur(head.left); // 2 postOrderRecur(head.right); // 3 System.out.println(head.value + " "); } ``` ## 所有递归都可以改为非递归 ![](https://www.www.zyiz.net/i/l/?n=22&i=blog/2827305/202205/2827305-20220503222203104-1510746552.png)

------------恢复内容结束------------



这篇关于二叉树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程