二叉树的先序,中序,后序递归和非递归实现
来源:互联网 发布:休闲食品数据 编辑:程序博客网 时间:2024/05/17 22:23
二叉树l结构类定义:
测试二叉树:
1
2 3
4 5 6 7
8 10
9
先序遍历
输出:
递归先序遍历:1 2 4 8 9 10 5 3 6 7
非递归先序遍历:1 2 4 8 9 10 5 3 6 7
中序遍历:
输出:
递归中序遍历:8 9 4 10 2 5 1 6 3 7
非递归中序遍历:8 9 4 10 2 5 1 6 3 7
后序遍历:
输出:
递归后序遍历:9 8 10 4 5 2 6 7 3 1
非递归后序遍历:9 8 10 4 5 2 6 7 3 1
public class TreeNode { int val ;TreeNode left ;TreeNode right ;public TreeNode(int val){this.val = val ;left = null ;right = null ;}}
测试二叉树:
1
2 3
4 5 6 7
8 10
9
先序遍历
public class PreOrder {//递归方法public static void preorder1(TreeNode root){if(root == null) return ;System.out.print(root.val + " ");if(root.left!=null) preorder1(root.left);if(root.right != null) preorder1(root.right);}//非递归方法public static void preorder2(TreeNode root){LinkedList<TreeNode> stack = new LinkedList<TreeNode>();if(root == null) return ;TreeNode p = root ;stack.push(p);while(!stack.isEmpty()){TreeNode temp = stack.pop();System.out.print(temp.val + " ");if(temp.right != null) stack.push(temp.right);if(temp.left != null) stack.push(temp.left);}}public static void main(String[] args){TreeNode root = GeneratorTree.getTree();System.out.print("递归先序遍历:");preorder1(root);System.out.println();System.out.print("非递归先序遍历:");preorder2(root);}}
输出:
递归先序遍历:1 2 4 8 9 10 5 3 6 7
非递归先序遍历:1 2 4 8 9 10 5 3 6 7
中序遍历:
public class InOrder {//递归遍历public static void inorder1(TreeNode root){if(root == null) return ;if(root.left!=null) inorder1(root.left);System.out.print(root.val + " ");if(root.right != null) inorder1(root.right);}//非递归遍历public static void inorder2(TreeNode root){LinkedList<TreeNode> stack = new LinkedList<TreeNode>();if(root == null ) return ;TreeNode p = root ; ;while(p != null || !stack.isEmpty()){if(p != null){stack.push(p);p = p.left;}else{p = stack.pop();System.out.print(p.val + " ");p = p.right ;}}}public static void main(String[] args){TreeNode root = GeneratorTree.getTree();System.out.print("递归中序遍历:");inorder1(root);System.out.println();System.out.print("非递归中序遍历:");inorder2(root);}}
输出:
递归中序遍历:8 9 4 10 2 5 1 6 3 7
非递归中序遍历:8 9 4 10 2 5 1 6 3 7
后序遍历:
public class PostOrder {//递归遍历public static void postorder1(TreeNode root){if(root == null) return ;if(root.left!=null) postorder1(root.left);if(root.right != null) postorder1(root.right);System.out.print(root.val + " ");}//非递归遍历public static void postorder2(TreeNode root){if(root == null) return ;TreeNode p = root ;TreeNode pre = p ; //记录最近出栈元素LinkedList<TreeNode> stack = new LinkedList<TreeNode>();boolean flag = true ; //标记变量,表示当前是继续入栈还是出栈while( p!= null || !stack.isEmpty()){if(p!=null && flag == true){stack.push(p);p = p.left ;}else{if(stack.isEmpty()) return ;p = stack.peek() ;if(p.right != null && p.right != pre){p = p.right ;flag = true ;}else{p = stack.pop();System.out.print(p.val + " ");pre = p ;flag = false ;}}}}public static void main(String[] args){TreeNode root = GeneratorTree.getTree();System.out.print("递归后序遍历:");postorder1(root);System.out.println();System.out.print("非递归后序遍历:");postorder2(root);}}
输出:
递归后序遍历:9 8 10 4 5 2 6 7 3 1
非递归后序遍历:9 8 10 4 5 2 6 7 3 1
0 0
- 二叉树的先序、中序、后序遍历的递归和非递归实现
- 二叉树的先序,中序,后序递归和非递归实现
- C++ 二叉树的构建,先序/中序/后序的递归/非递归实现
- 二叉树的先序、中序、后序的递归与非递归实现
- 递归和非递归的方式实现二叉树的先序、中序和后序遍历
- 二叉树的先序、中序、后序的递归和非递归算法
- 分别用递归和非递归方式实现二叉树的先序、中序和后序遍历
- 二叉树的创建和先序,中序,后序,递归,非递归遍历
- 二叉树的先序、中序、后序递归遍历和非递归遍历
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 二叉树的先序、中序和后序遍历,递归与非递归方式实现。
- 二叉树的先序、中序、后续遍历的递归和非递归实现
- 建立二叉树,实现二叉树的先序遍历、中序和后序遍历的非递归算法
- 二叉树的创建(先序创建的)及先序遍历 中序遍历 后序遍历的递归和非递归实现
- 二叉树的先序、中序、后序的递归及非递归实现,以及层次遍历的实现:
- 二叉树的先序、中序、后序遍历的非递归实现
- 二叉树非递归和递归遍历(先序,中序,后序)
- 二叉树遍历的非递归算法(先序、中序、后序)代码实现
- nginx源码分析--请求体的读取(2)
- QwtPlotLayout解读---from Qwt-6.1.1
- 我是没得事
- Windows下Git bash乱码问题
- 四数之和为target特定值的序列
- 二叉树的先序,中序,后序递归和非递归实现
- nginx源码分析--多阶段请求处理
- Ajax Json示例
- 循环链表实现约瑟夫环
- 数组大折腾(3)
- uva 11853 Paintball dfs找连通块
- 设置图像的感兴趣区域(opencv1.0)
- JSON.stringify 和 JSON.parse
- Spring笔记