首页 > 代码库 > [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
[LeetCode]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.
简单的思想:正序排列时,某节点的右节点一定是该节点的右子树,中序排列,某节点的左节点一定是该节点的左子树;
深搜递归
/** * 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[] preorder, int[] inorder) { return buildTree(preorder,inorder,0,preorder.length-1,0,inorder.length-1); } private TreeNode buildTree(int []preorder, int []inorder,int pst,int pend,int inst, int inend){ if(pst>pend||inst>inend||preorder.length<1){ return null; } TreeNode tn =new TreeNode(preorder[pst]); int index = -1; for(int i=inst;i<=inend;i++){ if(inorder[i] == preorder[pst]){ index = i; break; } } tn.left = buildTree(preorder,inorder,pst+1,pst+index-inst,inst,inst+index-1); tn.right = buildTree(preorder,inorder,pst+index-inst+1,pend,index+1,inend); return tn; } }
[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。