新博文地址:
[leetcode]Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Note:
You may assume that duplicates do not exist in the tree.
根据前序遍历和中序遍历构建二叉树,构建二叉树必须要有中序遍历,前序和后序可选其一。如果对建树过程不太熟悉的朋友,可以看这里。里面很详细的讲解了二叉树的重构过程。算法肯定是一样的,就不多说了,大家可以看这里,代码如下:
public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder.length <= 0){ return null; } if(preorder.length == 1){ return new TreeNode(preorder[0]); } TreeNode root = new TreeNode(preorder[0]); int[] leftInorder = getSubArray(inorder, root.val); int[] rightInorder = Arrays.copyOfRange(inorder, leftInorder.length + 1, inorder.length); int[] leftPreOrder = Arrays.copyOfRange(preorder, 1, leftInorder.length + 1); int[] rightPreOrder = Arrays.copyOfRange(preorder, leftPreOrder.length + 1, preorder.length); root.left = buildTree(leftPreOrder, leftInorder); root.right = buildTree(rightPreOrder, rightInorder); return root; } private int[] getSubArray(int[] inorder,int rootVal){ int length = 0; for(length = 0; length < inorder.length; length++){ if(inorder[length] == rootVal){ break; } } return Arrays.copyOf(inorder, length); }
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳...
Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary...
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
leetcode的题目:Balanced Binary Tree
105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-...
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
[105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-...
105从Preorder和Inorder Traversal.js构造二叉树 106从有序和后置Traversal.js构造二叉树 107二叉树级订单遍历II.js 108将排序后的数组转换为Binary Search Tree.js 大多数Water.js的11个容器 110平衡Binary Tree....
construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
BinaryTree.py是一个方便的工具,它可以构建和显示编码时所需的二叉树 演示 您需要在使用之前导入该类 import BinaryTree as bt 从值/对象列表或二叉树构造二叉树 t1 = bt.BinaryTree([1,2,3,4,5,'#',6,7,'#','#','#...
leetcode 树节点 二叉树中序遍历 :evergreen_tree: 给定一棵二叉树,返回其节点值的中序遍历。 Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] 跟进:递归解决方案是微不足道的,你能迭代吗? 中序遍历: ...
二叉树前序遍历,leetcode
leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 ...binary_tree_inorder_traversal binary_tree_level_order_traversal binary_tree_level_order_traversal_I
leetcode卡leetcode 二叉树卡片 LeetCode 二叉树卡片问题的章节智解
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...