面试准备之--二叉树的递归与非递归方式
来源:互联网 发布:治痘痘最好的方法知乎 编辑:程序博客网 时间:2024/05/18 18:42
思路请参考:http://www.nowcoder.com/courses/1/7/1
三种二叉树递归方式:
import java.util.*; /*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class TreeToSequence { public int[][] convert(TreeNode root) { // write code here List<TreeNode> preList = new LinkedList<>();//定义List List<TreeNode> inList = new LinkedList<>(); List<TreeNode> afterList = new LinkedList<>(); preOrder(root,preList);//先序遍历函数 inOrder(root,inList);//中序遍历函数 afterOrder(root,afterList);//后序遍历函数 int size = preList.size(); int[][] ans = new int[3][];//返回的二维数组 int[] pre = new int[size]; int[] in = new int[size]; int[] after = new int[size]; for(int i = 0;i < size;++ i){ pre[i] = preList.get(i).val; in[i] = inList.get(i).val; after[i] = afterList.get(i).val; } ans[0] = pre; ans[1] = in; ans[2] = after; return ans; } public void preOrder(TreeNode root,List<TreeNode> preList){ if(root == null) return; preList.add(root); preOrder(root.left,preList); preOrder(root.right,preList); } public void inOrder(TreeNode root,List<TreeNode> inList){ if(root == null) return; inOrder(root.left,inList); inList.add(root); inOrder(root.right,inList); } public void afterOrder(TreeNode root,List<TreeNode> afterList){ if(root == null) return; afterOrder(root.left,afterList); afterOrder(root.right,afterList); afterList.add(root); }}
二叉树的非递归方式
import java.util.*; /*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class TreeToSequence { public int[][] convert(TreeNode root) { // write code here LinkedList<Integer> left=new LinkedList<Integer>(); LinkedList<Integer> mid=new LinkedList<Integer>(); LinkedList<Integer> right=new LinkedList<Integer>(); firstPrint(root,left); midPrint(root,mid); afterPrint(root,right); int[][] res=new int[3][left.size()]; for(int i=0;i<left.size();i++){ res[0][i]=left.get(i); res[1][i]=mid.get(i); res[2][i]=right.get(i); } return res; } public void midPrint(TreeNode root,LinkedList<Integer> res){ if(root==null) return ; midPrint(root.left,res); res.add(root.val); midPrint(root.right,res); } public void firstPrint(TreeNode root,LinkedList<Integer> res){ if(root==null) return ; res.add(root.val); firstPrint(root.left,res); firstPrint(root.right,res); } public void afterPrint(TreeNode root,LinkedList<Integer> res){ if(root==null) return ; afterPrint(root.left,res); afterPrint(root.right,res); res.add(root.val); }}
0 0
- 面试准备之--二叉树的递归与非递归方式
- 面试准备--二叉树的非递归遍历(Java版)
- 二叉树的递归与非递归
- 二叉树的递归与非递归
- 二叉树的递归与非递归
- 二叉树三种遍历方式的递归与非递归实现
- 笔试面试算法经典--二叉树的镜像-递归与非递归实现(Java)
- 四种遍历二叉树的方式(递归,非递归)
- 二叉树的前中后序遍历,递归和非递归方式
- 二叉树的三种遍历方式的递归与非递归实现
- 二叉树的三种遍历方式(递归与非递归详细实现)
- 二叉树 递归 与 非递归 遍历
- 递归与非递归二叉树遍历
- 二叉树递归与非递归遍历
- 递归与非递归实现二叉树
- 二叉树递归与非递归遍历
- 二叉树递归与非递归遍历
- 二叉树递归与非递归遍历
- 暑假培训
- HBase官方文档 0.97
- 类别(Category)的作用(二)---对私有方法的前向引用
- zTree插件入门
- 连续子数组最大和问题
- 面试准备之--二叉树的递归与非递归方式
- Android仿天天果园Splash
- Html.DropDownList()的用法
- 高并发数据结构Disruptor解析(5)
- POJ 3461 Oulipo(KMP模板)
- 赛码网_在线编程_军训队列
- 写点什么好呢2? 钱、事业、婚姻、人生意义
- bzoj1433(网络流)
- v2ex上的最近火起来的基于QT5的截图工具Snipaste