Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
来源:互联网 发布:微信运动的数据来源 编辑:程序博客网 时间:2024/05/16 01:14
1 解题思想
这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 先序和中序中恢复二叉树 解题报告
和之前的那个不同的是,前序向前看,后序则是向后看,后序最后一个一定是根节点,中序中每一段,在后序中最后出现那个,也一定是根节点
btw:
如果没记错的话,前序和后续是不能恢复二叉树的,无论怎么恢复,都必须要现有中序
如果问题,请指教
2 原题
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.
3 AC解
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /** * post:left-right-root * inorder:left-root-right * */public class Solution { /** * 核心思想 * 同上题,只是需要记得postorder从后面开始,然后改一下位置参数就可以了 * */ public TreeNode buildTreeHelper(int[] inorder,int is,int ie,int[] preorder,int ps) { if(is>ie || ps < 0) return null; //首先从inorder中确定第一个,然后既可以划分左右子树 //System.out.println(ps); TreeNode root = new TreeNode(preorder[ps]); int rootAtIn = is; while(inorder[rootAtIn] != root.val) rootAtIn ++; //拆分成两个子问题 root.left = buildTreeHelper(inorder,is,rootAtIn - 1,preorder,ps-(ie- rootAtIn + 1)); root.right = buildTreeHelper(inorder,rootAtIn+1,ie,preorder,ps-1); return root; } public TreeNode buildTree(int[] inorder, int[] postorder) { return buildTreeHelper(inorder,0,inorder.length-1,postorder,postorder.length-1); }}
0 0
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- 根据中序和后序序列重建二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal 根据中序和后序序列生成二叉树(重重)
- LeetCode OJ 之 Construct Binary Tree from Inorder and Postorder Traversal (由二叉树的中序和后序序列构造二叉树)
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根据先序序列和中序序列恢复二叉树
- python 利用 xdrlib 操作追加excel一种思路
- java 中void的使用
- Java学习——JDK,JRE,JVM之间的联系
- 堆内存和栈内存
- Codeforces Round #371 (Div. 2) B. Filya and Homework(水题)
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
- 20160916IOS音频开发之资源与元数据,解决获取资源属性问题(一)
- python处理数据——去除字符串两端的引号
- 商品添加和减少
- android自定义view 模仿win10进度条
- 计算机的组成及其功能
- Linux笔记--shell总结(2)
- Python函数式编程指南(三):迭代器
- C#(asp.net)哈希表