添加leetcode 124 105题关于二叉树部分内容

This commit is contained in:
zhuyijun 2021-11-10 16:45:51 +08:00
commit 0d75cae7f2
6 changed files with 322 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
out/

32
.vscode/launch.json vendored Normal file
View 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.jsonlabel
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

90
.vscode/settings.json vendored Normal file
View 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
View 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"
}

View 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;
}

View 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);
}