二叉树的递归实现(先,中,后)
2016-06-23 15:15
183 查看
package erchashu; import java.util.ArrayList; import java.util.List; public class diguierchashu { public static List<TreeNode> list1 =new ArrayList<TreeNode>(); public static List<TreeNode> list2 =new ArrayList<TreeNode>(); public static List<TreeNode> list3 =new ArrayList<TreeNode>(); public static void main(String[] args) { TreeNode l1 =new TreeNode(1); TreeNode l2 =new TreeNode(2); TreeNode l3 =new TreeNode(3); TreeNode l4 =new TreeNode(4); TreeNode l5 =new TreeNode(5); TreeNode l6 =new TreeNode(6); TreeNode l7 =new TreeNode(7); l1.left =l2; l1.right =l3; l2.left =l4; l2.right =l5; l3.left =l6; l3.right =l7; List<List<TreeNode>> list =new ArrayList<List<TreeNode>>(); diguierchashu.convert(l1); list.add(list1); list.add(list2); list.add(list3); for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).size();j++){ System.out.print(list.get(i).get(j).val); } System.out.println("^_^"); } } public static void convert(TreeNode root) { xianxu(root); zhongxu(root); houxu(root); } public static void houxu(TreeNode root) { if(root == null){ return ; } houxu(root.left); houxu(root.right); list3.add(root); } public static void zhongxu(TreeNode root) { if(root == null){ return ; } zhongxu(root.left); list2.add(root); zhongxu(root.right); } public static void xianxu(TreeNode root) { if(root == null){ return ; } list1.add(root); xianxu(root.left); xianxu(root.right); } static class TreeNode{ int val = 0; TreeNode left = null; TreeNode right = null; TreeNode(int val){ this.val = val; } } }
相关文章推荐
- BZOJ3060: [Poi2012]Tour de Byteotia
- 使用UITextFiled收回键盘的几种方法
- Linux上安装Python的PIL和Pillow库处理图片的实例教程
- JavaEE_SAX
- jQuery练习一好友列表变色
- Python-S13作业-day1-之登陆程序
- 第13周 关于二叉树的遍历方法
- In Gradle projects, always use http://schemas.android.com/apk/res-auto for custom attributes
- android TextView 垂直自动滚动
- Android中正确保存view的状态
- CSS Secret——CSS的浏览器兼容
- 系统内置的一些工具类
- python 矩阵转置
- mysql安装配置详解教程(一)
- Error:Failed to create directory 'C:\Users\user\.gradle\caches\2.10\scripts\ijinit6_904ystmpz5u5lc7u
- android studio 快捷键修改
- Volley 源码解析
- 安卓多行文本折叠展开效果
- ssm小知识汇总
- 12C cdb同pdb字符集不同,导致pdb无法启动