Want to know:
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; }
Get a detailed, AI-powered explanation for this question and thousands more on StudyFetch.
Get the Answer for FreeHow StudyFetch Helps You Master This Topic
AI-Powered Answers
Get instant, detailed explanations powered by AI that understands your course material.
Deep Understanding
Go beyond surface-level answers with step-by-step breakdowns and examples.
Personalized Learning
Spark.E adapts to your learning style and helps you connect ideas.
Practice & Test
Turn any question into flashcards, quizzes, and practice tests to solidify your knowledge.
Explore More Questions
- Quelle est la relation entre la distribution t de Student et la distribution de FisherSnedecor? ❑ Si Z ~ F(1,r), alors Z ~ t(r). ❑ Si Z ~ t2 (r), Z ~ F(r,1). ❑ Si Z ~ F(1,r), alors Z ~ t2 (r). ❑ Il manque des informations pour déterminer la nature exacte de la relation.
- Quand je propage la formule =D12 * B$12 sur la ligne 13, que devient-elle?
- Que peut-on dire si deux droites sont parallèles entre elles et si une troisième droite est perpendiculaire à l'une ?