File tree Expand file tree Collapse file tree 1 file changed +6
-23
lines changed
algorithms/000889-construct-binary-tree-from-preorder-and-postorder-traversal Expand file tree Collapse file tree 1 file changed +6
-23
lines changed Original file line number Diff line number Diff line change @@ -69,29 +69,12 @@ class TreeNode {
69
69
* @return TreeNode
70
70
*/
71
71
function constructFromPrePost($preorder, $postorder) {
72
- if (empty($preorder)) {
73
- return null;
74
- }
75
- $rootVal = $preorder[0];
76
- $root = new TreeNode($rootVal);
77
- $n = count($preorder);
78
- if ($n == 1) {
79
- return $root;
80
- }
81
- $leftVal = $preorder[1];
82
- $leftPostIdx = array_search($leftVal, $postorder);
83
- $leftSize = $leftPostIdx + 1;
84
-
85
- $leftPre = array_slice($preorder, 1, $leftSize);
86
- $leftPost = array_slice($postorder, 0, $leftSize);
87
-
88
- $rightPre = array_slice($preorder, $leftSize + 1);
89
- $rightPost = array_slice($postorder, $leftSize, $n - $leftSize - 1);
90
-
91
- $root->left = constructFromPrePost($leftPre, $leftPost);
92
- $root->right = constructFromPrePost($rightPre, $rightPost);
93
-
94
- return $root;
72
+ ...
73
+ ...
74
+ ...
75
+ /**
76
+ * go to ./solution.php
77
+ */
95
78
}
96
79
97
80
// Helper function to print tree in level-order
You can’t perform that action at this time.
0 commit comments