/** * Definition for binary tree public class TreeNode { int val; TreeNode left; * TreeNode right; TreeNode(int x) { val = x; } } */ public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if (inorder == null || postorder == null || inorder.length != postorder.length) return null; int n = inorder.length; return buildTree(inorder, postorder, 0, n - 1, 0, n - 1); } // 根据中序遍历和后序遍历生成二叉树 dfs public TreeNode buildTree(int[] inorder, int[] postorder, int s1, int e1, int s2, int e2) { if (s1 > e1 || s2 > e2) return null; if (s2 == e2) return new TreeNode(postorder[s2]); // 后序遍历数组的最后一个是根节点 int rootval = postorder[e2]; TreeNode root = new TreeNode(rootval); int i; // 根据根节点在中序遍历数组里面得到左子树和右子数节点的数目 for (i = s1; i <= e1; i++) { if (inorder[i] == rootval) break; } int leftlength = i - s1; int rightlength = e1 - i; // dfs递归 root.left = buildTree(inorder, postorder, s1, i - 1, s2, s2 + leftlength - 1); root.right = buildTree(inorder, postorder, i + 1, e1, e2 - rightlength, e2 - 1); return root; } public int getlength(TreeNode root) { int depthLeft = 0; int depthRight = 0; int depth = 0; // 左子树的深度 if (root.left != null) { depthLeft = getlength(root.left) + 1; } // 右子树的深度 if (root.right != null) { depthRight = getlength(root.right) + 1; } if (depthLeft >= depthRight) { depth = depthLeft; } else { depth = depthRight; } return depth; } public static void main(String[] args) { String in = "T,b,H,V,h,3,o,g,P,W,F,L,u,A,f,G,r,m,1,x,J,7,w,e,0,i,Q,Y,n,Z,8,K,v,q,k,9,y,5,C,N,B,D,2,4,U,l,c,p,I,E,M,a,j,6,S,R,O,X,s,d,z,t"; String po = "T,V,H,o,3,h,P,g,b,F,f,A,u,m,r,7,J,x,e,w,1,Y,Q,i,0,Z,n,G,L,K,y,9,k,q,v,N,D,B,C,5,4,c,l,U,2,8,E,I,R,S,6,j,d,s,X,O,a,M,p,W,t,z"; String[] inChar = in.split(","); String[] poChar = po.split(","); System.out.println(inChar.length); System.out.println(poChar.length); int[] inI = new int[inChar.length]; int[] poI = new int[poChar.length]; for (int i = 0; i < inChar.length; i++) { inI[i] = inChar[i].toCharArray()[0]; poI[i] = poChar[i].toCharArray()[0]; } TreeNode tree = new Solution().buildTree(inI, poI); System.out.println(new Solution().getlength(tree)); // for(int i=0;i<inChar.length;i++){ // System.out.print(inI[i]+" "); // } // System.out.println(); // for(int i=0;i<inChar.length;i++){ // System.out.print(poI[i]+" "); // } } } // Definition for binary tree class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
相关推荐
用C++写的,包括二叉树的构建,二叉树的先序遍历、中序遍历和后序遍历非递归算法。
给出二叉树的中序遍历序列和后序遍历序列,编程还原该二叉树。 输入: 第1行为二叉树的中序遍历序列 第2行为二叉树的后序遍历序列 输出: 二叉树的按层遍历序列
二叉树的遍历,层次遍历输入,输出前序,中序,后序,MFC界面实现
主要介绍了Python二叉树的遍历操作,结合实例形式分析了Python针对二叉树的前序遍历,中序遍历,后序遍历,层序遍历等相关操作实现技巧,需要的朋友可以参考下
二叉树、二叉搜索树的构建,前序、中序、后序的 递归和非递归遍历;前序中序序列构建二叉树……
C语言实现数据结构中根据中序、后序遍历构建树,并进行先序与层次遍历,以树形结构输出,附有详细中文注释
先序遍历中序遍历后序遍历图解
实现链式存储二叉树构建,完成查找、求树高度、中序遍历、先遍历、后序遍历和层序遍历的程序,给出算法的时间和空间复杂度。
[问题描述] 如果给出了遍历二叉树的前序序列和中序序列,则可以构造出唯一的一棵二叉树。试编写实现上述功能的程序。...(3)对该二叉树进行后序遍历,输出后序遍历序列。 (4)用凹入法输出该二叉树。
通过本次实习加强了对二叉树的建立和各种遍历...它们分别完成了二叉树的建立,以及递归、非递归的先序遍历、中序遍历、后序遍历和层序遍历算法:其中先序中序后序的递归遍历算法是利用二叉树的链式存储结构进行的遍历。
此时构建当前节点,并递归建立左右子树,在左右子树对应位置继续递归遍历进行上述步骤,直到节点为空,具体操作步骤如下:从后序遍历顺序中当前根节点的位置在 posto
二叉树深度 ...二叉树中序遍历: 递归实现 非递归实现 二叉树后序遍历: 递归实现 非递归实现 二叉树层次遍历 二叉树层次创建,创建方法遵循卡特兰数 http://write.blog.csdn.net/postedit/17380455
使用composite模式构成二叉树,并用迭代器模式封装访问,前序、中序和后序的遍历。JAVA 编写。 Main中直接运行
线索化二叉树之先序,中序,后序线索建树,先序,后序,中序遍历
实现由先序、中序序列构造二叉树,由后序、中序序列构造二叉树,广度优先遍历以root为根结点的子树,中序遍历(递归,非递归)以root为根结点的子树
二叉树的构建及遍历操作,使用链表的形式构建二叉树并进行前序、中序、后序遍历操作
本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历、中序遍历、后序遍历、层次遍历),二叉搜索树的构造等。 1. 二叉树的构建 二叉树的基本构建方式为:添加一个节点,如果这是一棵...
可实现以下功能: ... 4---中序遍历(递归) 5---中序遍历(非递归) 6---后序遍历(递归) 7---后序遍历(非递归) 8---求树高 9---求叶子总数 10---输出二叉树 11---交换左右子树 0---退出
4. 掌握树和二叉树的逻辑结构特点、二叉树的顺序存储结构、二叉树的链式存储结构——二叉链表、二叉链表的结构体类型定义、在二叉链表存储结构上先序遍历、中序遍历、后序遍历的实现算法。 实验内容 1、以字符串...
本篇功能:python实现将前序序列构建二叉树。输出二叉树的中序和后续排列。 二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有深度遍历和广度遍历,深度遍历有前序...