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

Commit a32aaa5

Browse files
Add solution
1 parent adb12b6 commit a32aaa5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import UIKit
2+
3+
/*
4+
Given inorder and postorder traversal of a tree, construct the binary tree.
5+
6+
Note:
7+
You may assume that duplicates do not exist in the tree.
8+
9+
For example, given
10+
11+
inorder = [9,3,15,20,7]
12+
postorder = [9,15,7,20,3]
13+
Return the following binary tree:
14+
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
*/
21+
22+
public class TreeNode {
23+
public var val: Int
24+
public var left: TreeNode?
25+
public var right: TreeNode?
26+
public init() { self.val = 0; self.left = nil; self.right = nil; }
27+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
28+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
29+
self.val = val
30+
self.left = left
31+
self.right = right
32+
}
33+
34+
func printTreeInorder(_ node: TreeNode?) {
35+
if node == nil { return }
36+
if node!.left != nil {
37+
printTreeInorder(node!.left)
38+
}
39+
40+
if node!.right != nil {
41+
printTreeInorder(node!.right)
42+
}
43+
print(node!.val)
44+
}
45+
}
46+
47+
class Solution {
48+
func buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? {
49+
if inorder.count == 0 {return nil}
50+
if inorder.count == 1 {
51+
return TreeNode(inorder.first!)
52+
}
53+
54+
let rootIndex = postorder.count - 1
55+
var index = 0
56+
while index < inorder.count {
57+
if inorder[index] == postorder[rootIndex] {
58+
break
59+
} else {
60+
index += 1
61+
}
62+
}
63+
64+
let root = TreeNode(inorder[index])
65+
let left = buildTree(Array(inorder[0 ..< index]),Array(postorder[0 ..< index]))
66+
root.left = left
67+
68+
if index != inorder.count - 1 {
69+
let right = buildTree(Array(inorder[index+1 ..< inorder.count]),Array(postorder[index ..< inorder.count - 1]))
70+
root.right = right
71+
}
72+
73+
return root
74+
}
75+
}
76+
77+
import Foundation
78+
import XCTest
79+
class SolutionTests: XCTestCase {
80+
var solution: Solution!
81+
override func setUp() {
82+
super.setUp()
83+
solution = Solution()
84+
}
85+
86+
func test1() {
87+
let tree = solution.buildTree([4,9,5,3,15,20,7], [4,5,9,15,7,20,3])
88+
// XCTAssertEqual(input, result)
89+
print(tree?.printTreeInorder(tree))
90+
}
91+
}
92+
93+
SolutionTests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

‎106. Construct Binary Tree from Inorder and Postorder Traversal.playground/playground.xcworkspace/contents.xcworkspacedata‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /