编程之美--重建二叉树
2012-10-28 12:18
281 查看
/** * 重建二叉树 给两个序列:pre: a b d c e f in : d b a e c f 把这个二叉树创建出来 */ public class ReBuilderTree { private Entry root; public void rebuild(String pre, String in) { root = rebuildHelper(pre, in); } private Entry rebuildHelper(String pre, String in) { if (pre == null || pre.length() == 0 || in == null || in.length() == 0) return null; char c = pre.charAt(0); int t = in.indexOf(c); // 找到前充的第一个在中充中的位置 Entry newEntry = new Entry(c, null, null); newEntry.left = rebuildHelper(pre.substring(1, t + 1), in.substring(0, t + 1)); newEntry.right = rebuildHelper(pre.substring(t + 1), in.substring(t + 1)); return newEntry; } private void preOrder(Entry e) { } static class Entry { char val; Entry left; Entry right; public Entry(char val, Entry left, Entry right) { this.val = val; this.left = left; this.right = right; } } public static void main(String args[]) { String pre = "abdcef", in = "dbaecf"; ReBuilderTree object = new ReBuilderTree(); object.rebuild(pre, in); } }
相关文章推荐
- 重建二叉树_《编程之美》3.9
- 重建二叉树(编程之美)
- 编程之美——3.9重建二叉树和3.10分层遍历二叉树(Java and C++)
- 重建二叉树(剑指offer6、编程之美3.9)
- 《编程之美》读书笔记19: 3.9 重建二叉树
- 编程之美--根据遍历结果重建二叉树
- 重建二叉树(编程之美3.9)
- 二叉树的重建(3种)---编程之美
- 《编程之美》3.9重建二叉树
- 编程之美系列: 3.9 重建二叉树
- 编程之美:第三章 结构之法 3.9重建二叉树
- 《编程之美》3.9重建二叉树
- 编程之美--重建二叉树
- 《编程之美》重建二叉树
- 编程之美3.9 重建二叉树
- 《编程之美》——重建二叉树
- 二叉树的遍历;前序 中序 后序遍历二叉树;递归 非递归实现; 重建二叉树;编程之美重建二叉树
- nyoj重建二叉树(不真的建立)
- 剑指offer---重建二叉树
- NYOJ221tree(重建二叉树)