
Do you need help with
hashmap记录inorder值于index的对应然后dfs,preorder第一个为root然后通过map找到root的index,和inStart,inEnd比较的到左右的大小然后再在preorder里找到根据左右大小找到left的root和right的root,如此迭代 public TreeNode build(int pre, int inStart, int inEnd, int[] preorder, HashMap<Integer,Integer> map){ if(pre >= preorder.length || inStart > inEnd) { return null; } TreeNode root = new TreeNode(preorder[pre]); if(inStart == inEnd) { return root; } int index = map.get(preorder[pre]); int leftSize = index - inStart; int rightSize = inEnd - index; int leftRoot = pre + 1; int rightRoot = pre + leftSize + 1; root.left = build(leftRoot, inStart, index-1, preorder, map); root.right = build(rightRoot, index+1, inEnd, preorder, map); return root; }
Then try StudyFetch, the AI-powered platform that can answer your questions and teach you more about it!


How StudyFetch Helps You Master This Topic
AI-Powered Explanations
Get in-depth, personalized explanations on this topic and related concepts, tailored to your learning style.
Practice Tests
Take adaptive quizzes that focus on your weak areas and help reinforce your understanding of the subject.
Interactive Flashcards
Review key concepts and terms with AI-generated flashcards, optimizing your retention and recall.
Educational Games
Engage with fun, interactive games that reinforce your learning and make studying more enjoyable.
Start mastering this topic and many others with StudyFetch's comprehensive learning tools.