105. Construct Binary Tree from Preorder and Inorder Traversal
Posted 鸵鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了105. Construct Binary Tree from Preorder and Inorder Traversal相关的知识,希望对你有一定的参考价值。
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length); } private TreeNode buildTree(int[] preorder, int l1, int r1, int[] inorder, int l2, int r2){ if(l1>r1||l2>r2) return null; int idx=l2; while(inorder[idx]!=preorder[l1]) idx++; TreeNode node=new TreeNode(preorder[l1]); node.left=buildTree(preorder, l1+1, l1+(idx-l2), inorder, l2, idx-1); node.right=buildTree(preorder, l1+(idx-l2)+1, r1, inorder, idx+1, r2); return node; } }
以上是关于105. Construct Binary Tree from Preorder and Inorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章
105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal
105.Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal