package part7tree;import java.util.ArrayList;/*** 递归的方式依次返回二叉树的前序、中序、后序* Created by Dell on 2017年07月26日.*/public class TreeToSequence {public int[][] convert(TreeNode root) {int[][] res=new int[3][];ArrayList<Integer> pre=new ArrayList<Integer>();ArrayList<Integer> in=new ArrayList<Integer>();ArrayList<Integer> post=new ArrayList<Integer>();preOrder(root,pre);inOrder(root,in);postOrder(root,post);res[0]=transeArray(pre);res[1]=transeArray(in);res[2]=transeArray(post);return res;}private void preOrder(TreeNode root, ArrayList<Integer> pre) {if(root!=null){pre.add(root.val);preOrder(root.left,pre);preOrder(root.right,pre);}}private void inOrder(TreeNode root, ArrayList<Integer> in) {if(root!=null){inOrder(root.left,in);in.add(root.val);inOrder(root.right,in);}}private void postOrder(TreeNode root, ArrayList<Integer> post) {if(root!=null){postOrder(root.left,post);postOrder(root.right,post);post.add(root.val);}}private int[] transeArray(ArrayList<Integer> list){int[] res=new int[list.size()];int count=0;for(Integer i:list){res[count++]=i;}return res;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。