微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

二叉树

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

递归序 每个节点都会 来到 三次 然后根据在每次来的时候,在哪一次操作,分为三种遍历,都是基于根节点为参考 - 先序, 头 左 右 - 中序, 左 头 右 - 后序, 左 右 头 递归实现 ```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.icode9.com/i/l/?n=22&i=blog/2827305/202205/2827305-20220503222203104-1510746552.png)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐