LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 题解
来源:互联网 发布:制药工程就业前景知乎 编辑:程序博客网 时间:2024/06/13 00:05
题目链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/?tab=Description
思路:使用递归实现,想明白这里还比较容易,个人觉得难点在于如何设计这个递归方法,它的参数应该有哪些:也就是应该用哪些参数来界定子问题。
该算法的关键是
public TreeNode build(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd)
该递归方法有六个参数:前序遍历和中序遍历数组,以及每个子树在这两个数组中对应的起始位置。
难点二在于:左右子树的参数值应该是什么,这几个参数值,结合网上的博客加上自己的思考也不是很难理解。
Java代码:
public class Solution {/** * 使用前序遍历和中序遍历数组递归构造二叉树的递归方法实现 * @param preorder 前序遍历数组 * @param preStart preStart用于确定这个子树的前序遍历序列在原始前序遍历数组中的起始位置 * @param preEnd preEnd用于确定这个子树的前序遍历序列在原始前序遍历数组中的结束位置 * @param inorder 中序遍历数组 * @param inStart inStart用于确定这个子树的中序遍历序列在原始中序遍历数组中的起始位置 * @param inEnd inEnd用于确定这个子树的中序遍历序列在原始中序遍历数组中的结束位置 * @return */public TreeNode build(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {// 递归结束的条件(越界 or 子树长度小于1)if (preStart >= preorder.length || preEnd < preStart) {return null;}TreeNode root = new TreeNode(preorder[preStart]);int index = 0;// 获取中序遍历中根节点的下标for (int i = inStart; i <= inEnd; i++) {if (inorder[i] == preorder[preStart]) {index = i;break;}}// 子树节点长度int len = index - inStart;// 重点是preStart、preEnd、inStart、 inEnd四个参数的取值root.left = build(preorder, preStart + 1, preStart + len, inorder, inStart, index - 1);root.right = build(preorder, preStart + len + 1, preEnd, inorder, index + 1, inEnd);return root;}public TreeNode buildTree(int[] preorder, int[] inorder) {if (preorder == null || inorder == null) {return null;}if (preorder.length == 0 || inorder.length == 0) {return null;}return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);}}
0 0
- LeetCode题解: Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode题解:Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode题解:Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode *** 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-105.Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- ajax 操作/同步、异步区别
- 从ML到HTML到XHTML再到XML
- 文本文件序列化和二进制序列化
- c3p0和QueryRunner的结合使用,让开发更加简便
- CSS盒子模型
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 题解
- Ubuntu 更改权限问题 只能以guest访问
- C/C++_log2000_完美立方
- Android判断输入密码的强度
- U盘装系统
- C++第一次作业
- http请求(Python爬虫headers)
- mysql无法启动成功,1067错误
- SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)