添加leetcode 124 105题关于二叉树部分内容
This commit is contained in:
commit
0d75cae7f2
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
out/
|
32
.vscode/launch.json
vendored
Normal file
32
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Launch",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceRoot}/out/${fileBasenameNoExtension}",
|
||||
// 此处若可执行文件不想放在一个文件夹中,则把 /out 删去即可
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceRoot}",
|
||||
// 此处${workspaceRoot}与${workspaceFolder}应该类似,但网上资料大部分用的前者
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
],
|
||||
"preLaunchTask": "C/C++: g++ build active file",
|
||||
// 此处要与tasks.json中的label相同
|
||||
"miDebuggerPath": "/usr/bin/gdb"
|
||||
}
|
||||
]
|
||||
}
|
90
.vscode/settings.json
vendored
Normal file
90
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iostream": "cpp",
|
||||
"ostream": "cpp",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cctype": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"complex": "cpp",
|
||||
"concepts": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"coroutine": "cpp",
|
||||
"csetjmp": "cpp",
|
||||
"csignal": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cuchar": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"regex": "cpp",
|
||||
"source_location": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"future": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ranges": "cpp",
|
||||
"scoped_allocator": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"thread": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp",
|
||||
"barrier": "cpp",
|
||||
"latch": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"syncstream": "cpp"
|
||||
}
|
||||
}
|
38
.vscode/tasks.json
vendored
Normal file
38
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "shell",
|
||||
// 此处type的属性问题下面详说
|
||||
"label": "C/C++: g++ build active file",
|
||||
"command": "/usr/bin/g++",
|
||||
"presentation": {
|
||||
"echo": true,
|
||||
"reveal": "always",
|
||||
"focus": false,
|
||||
"panel": "shared",
|
||||
"showReuseMessage": true,
|
||||
"clear": false
|
||||
},
|
||||
"args": [
|
||||
"-g",
|
||||
"${file}",
|
||||
"-std=c++17",
|
||||
"-o",
|
||||
"${workspaceRoot}/out/${fileBasenameNoExtension}"
|
||||
],
|
||||
// 此处与launch.json中类似,若不需要则删去 /out
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
95
tree/Binary_tree/leetcode_105/main.cpp
Normal file
95
tree/Binary_tree/leetcode_105/main.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: 1.0
|
||||
* @Autor: zhuyijun
|
||||
* @Date: 2021-11-10 13:09:52
|
||||
* @LastEditTime: 2021-11-10 16:39:11
|
||||
*/
|
||||
/*
|
||||
题目: 给定一棵树的前序遍历 preorder 与中序遍历
|
||||
inorder。请构造二叉树并返回其根节点。
|
||||
|
||||
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
|
||||
Output: [3,9,20,null,null,15,7]
|
||||
|
||||
示例 2:
|
||||
Input: preorder = [-1], inorder = [-1]
|
||||
Output: [-1]
|
||||
|
||||
提示:
|
||||
1 <= preorder.length <= 3000
|
||||
inorder.length == preorder.length
|
||||
-3000 <= preorder[i], inorder[i] <= 3000
|
||||
preorder 和 inorder 均无重复元素
|
||||
inorder 均出现在 preorder
|
||||
preorder 保证为二叉树的前序遍历序列
|
||||
inorder 保证为二叉树的中序遍历序列
|
||||
*/
|
||||
|
||||
// 前序:root -> left -> right 中序 left->root->right 后序: right->left->root
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
TreeNode *pre_order(vector<int> &pre, int leftpre, int rightpre,
|
||||
vector<int> &in, int leftin, int rightin) {
|
||||
if (leftpre > rightpre || leftin > rightin) return nullptr;
|
||||
|
||||
TreeNode *root = new TreeNode(pre[leftpre]);
|
||||
|
||||
int rootin = leftin;
|
||||
|
||||
while (rootin <= rightin && in[rootin] != pre[leftpre]) rootin++;
|
||||
int left = rootin - leftin;
|
||||
|
||||
root->left =
|
||||
pre_order(pre, leftpre + 1, leftpre + left, in, leftin, rootin - 1);
|
||||
|
||||
root->right =
|
||||
pre_order(pre, leftpre + left + 1, rightpre, in, rootin + 1, rightin);
|
||||
return root;
|
||||
}
|
||||
|
||||
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
|
||||
return pre_order(preorder, 0, preorder.size() - 1, inorder, 0,
|
||||
inorder.size() - 1);
|
||||
}
|
||||
|
||||
void printTree(TreeNode *root, vector<int> *res) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
res->push_back(root->val);
|
||||
printTree(root->left, res);
|
||||
printTree(root->right, res);
|
||||
}
|
||||
int main() {
|
||||
vector<int> pre;
|
||||
pre.push_back(3);
|
||||
pre.push_back(9);
|
||||
pre.push_back(20);
|
||||
pre.push_back(15);
|
||||
pre.push_back(7);
|
||||
|
||||
vector<int> in;
|
||||
in.push_back(9);
|
||||
in.push_back(3);
|
||||
in.push_back(15);
|
||||
in.push_back(20);
|
||||
in.push_back(7);
|
||||
TreeNode *root = buildTree(pre, in);
|
||||
vector<int> res;
|
||||
printTree(root, &res);
|
||||
for (int i = 0; i < res.size(); i++) {
|
||||
std::cout << res[i] << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
66
tree/Binary_tree/leetcode_124/main.cpp
Normal file
66
tree/Binary_tree/leetcode_124/main.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: 1.0
|
||||
* @Autor: zhuyijun
|
||||
* @Date: 2021-11-10 09:09:12
|
||||
* @LastEditTime: 2021-11-10 13:21:51
|
||||
*/
|
||||
// 路径
|
||||
// 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。
|
||||
//同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个
|
||||
//节点,且不一定经过根节点。
|
||||
// 路径和 是路径中各节点值的总和。
|
||||
|
||||
// 给你一个二叉树的根节点 root ,返回其 最大路径和 。
|
||||
|
||||
//
|
||||
|
||||
// 示例 1:
|
||||
|
||||
// 输入:root = [1,2,3]
|
||||
// 输出:6
|
||||
// 解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
|
||||
// 示例 2:
|
||||
|
||||
// 输入:root = [-10,9,20,null,null,15,7]
|
||||
// 输出:42
|
||||
// 解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
|
||||
//
|
||||
|
||||
// 提示:
|
||||
|
||||
// 树中节点数目范围是 [1, 3 * 104]
|
||||
// -1000 <= Node.val <= 1000
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
int maxPathSum(TreeNode *root, int &val) {
|
||||
if (root == nullptr) return 0;
|
||||
int left = maxPathSum(root->left, val);
|
||||
int right = maxPathSum(root->right, val);
|
||||
int lmr = root->val + max(0, left) + max(0, right);
|
||||
int ret = root->val + max(0, max(left, right));
|
||||
val = max(val, max(lmr, ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
int maxPathSum(TreeNode *root) {
|
||||
int val = INT_MIN;
|
||||
maxPathSum(root, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
int main() {
|
||||
TreeNode root = TreeNode();
|
||||
std::cout << maxPathSum(&root);
|
||||
}
|
Loading…
Reference in New Issue
Block a user