添加(0226.翻转二叉树.md):增加typescript版本

This commit is contained in:
Steve2020
2022-01-29 22:12:49 +08:00
parent 97bc7efbb2
commit be3f2cfc8a

View File

@ -563,7 +563,135 @@ var invertTree = function(root) {
}; };
``` ```
### TypeScript
递归法:
```typescript
// 递归法(前序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
invertTree(root.left);
invertTree(root.right);
return root;
};
// 递归法(后序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
invertTree(root.left);
invertTree(root.right);
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
return root;
};
// 递归法(中序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
invertTree(root.left);
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
// 因为左右节点已经进行交换此时的root.left 是原先的root.right
invertTree(root.left);
return root;
};
```
迭代法:
```typescript
// 迭代法(栈模拟前序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: TreeNode[] = [];
let curNode: TreeNode,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
// 入栈操作最好在交换节点之前进行,便于理解
if (curNode.right) helperStack.push(curNode.right);
if (curNode.left) helperStack.push(curNode.left);
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
return root;
};
// 迭代法(栈模拟中序遍历-统一写法形式)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: (TreeNode | null)[] = [];
let curNode: TreeNode | null,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop();
if (curNode !== null) {
if (curNode.right !== null) helperStack.push(curNode.right);
helperStack.push(curNode);
helperStack.push(null);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
}
return root;
};
// 迭代法(栈模拟后序遍历-统一写法形式)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: (TreeNode | null)[] = [];
let curNode: TreeNode | null,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop();
if (curNode !== null) {
helperStack.push(curNode);
helperStack.push(null);
if (curNode.right !== null) helperStack.push(curNode.right);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
}
return root;
};
// 迭代法(队列模拟层序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
const helperQueue: TreeNode[] = [];
let curNode: TreeNode,
tempNode: TreeNode | null;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
curNode = helperQueue.shift()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
if (curNode.left !== null) helperQueue.push(curNode.left);
if (curNode.right !== null) helperQueue.push(curNode.right);
}
}
return root;
};
```
### C: ### C:
递归法 递归法
```c ```c
struct TreeNode* invertTree(struct TreeNode* root){ struct TreeNode* invertTree(struct TreeNode* root){