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

feat: add solutions to lc problem: No.1028 #4099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into doocs:main from rain84:feature/lc-1028
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,92 @@ public:
};
```

#### TypeScript

```ts
function recoverFromPreorder(traversal: string): TreeNode | null {
const stack: TreeNode[] = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}
```

#### JavaScript

```js
function recoverFromPreorder(traversal) {
const stack = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,92 @@ public:
};
```

#### TypeScript

```ts
function recoverFromPreorder(traversal: string): TreeNode | null {
const stack: TreeNode[] = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}
```

#### JavaScript

```js
function recoverFromPreorder(traversal) {
const stack = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function recoverFromPreorder(traversal) {
const stack = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function recoverFromPreorder(traversal: string): TreeNode | null {
const stack: TreeNode[] = [];
let i = 0;

while (i < traversal.length) {
let depth = 0;
while (i < traversal.length && traversal[i] === '-') {
depth++;
i++;
}

let num = 0;
while (i < traversal.length && !Number.isNaN(+traversal[i])) {
num = num * 10 + +traversal[i];
i++;
}

// Create the new node
const newNode = new TreeNode(num);

while (stack.length > depth) {
stack.pop();
}

if (stack.length > 0) {
const i = stack.length - 1;
if (stack[i].left === null) {
stack[i].left = newNode;
} else {
stack[i].right = newNode;
}
}

stack.push(newNode);
}

return stack.length ? stack[0] : null;
}

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