Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2f257e

Browse files
Create 106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal.java
1 parent2c59f47 commitb2f257e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//This video helped https://www.youtube.com/watch?v=LgLRTaEMRVc&ab_channel=takeUforward
2+
3+
classSolution {
4+
publicTreeNodebuildTree(int[]inorder,int[]postorder) {
5+
HashMap<Integer,Integer>inMap =newHashMap<>();
6+
for (inti =0;i<inorder.length;i++) {
7+
inMap.put(inorder[i],i);
8+
}
9+
returnhelper(inorder,0,inorder.length-1,postorder,0,postorder.length-1,inMap);
10+
}
11+
12+
publicTreeNodehelper(int[]inorder,intiStart,intiEnd,int[]postorder,intpStart,intpEnd,HashMap<Integer,Integer>inMap) {
13+
if (pStart>pEnd ||iStart>iEnd) {
14+
returnnull;
15+
}
16+
TreeNoderoot =newTreeNode(postorder[pEnd]);
17+
intindex =inMap.get(postorder[pEnd]);
18+
intnumsLeft =index-iStart;
19+
root.left =helper(inorder,iStart,index-1,postorder,pStart,pStart+numsLeft-1,inMap);
20+
root.right =helper(inorder,index+1,iEnd,postorder,pStart+numsLeft,pEnd-1,inMap);
21+
returnroot;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp