Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Jul 29, 2023
1 parent 230402e commit 12fc132
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
6 changes: 5 additions & 1 deletion notes/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@
- [day 17](./day17.md)
- [110. 平衡二叉树](./day17/lc110.md)
- [257. 二叉树的所有路径](./day17/lc257.md)
- [404. 左叶子之和](./day17/lc404.md)
- [404. 左叶子之和](./day17/lc404.md)
- [day 18](./day18.md)
- [513. 找树左下角的值](./day18/lc513.md)
- [112. 路径总和](./day18/lc112.md)
= [106. 从中序与后序遍历序列构造二叉树](./day18/lc106.md)
30 changes: 30 additions & 0 deletions notes/src/day18.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 第六章 二叉树 part05
今日内容

● 513.找树左下角的值
● 112. 路径总和 113.路径总和ii
● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

详细布置

## 找树左下角的值

本地递归偏难,反而迭代简单属于模板题, 两种方法掌握一下

题目链接/文章讲解/视频讲解:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html

## 路径总和

本题 又一次设计要回溯的过程,而且回溯的过程隐藏的还挺深,建议先看视频来理解

112. 路径总和,和 113. 路径总和ii 一起做了。 优先掌握递归法。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html

## 从中序与后序遍历序列构造二叉树

本题算是比较难的二叉树题目了,大家先看视频来理解。

106.从中序与后序遍历序列构造二叉树,105.从前序与中序遍历序列构造二叉树 一起做,思路一样的

题目链接/文章讲解/视频讲解:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html
39 changes: 39 additions & 0 deletions notes/src/day18/lc106.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 106. 从中序与后序遍历序列构造二叉树

## 题目描述
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

## 解题思路

```cpp
class Solution {
public:
TreeNode* buildTree(vector<int> ino, vector<int> posto) {
if(ino.empty())return NULL;
int l=posto.back();
auto it=find(ino.begin(),ino.end(),l);
int i =distance(ino.begin(),it);
TreeNode*left=buildTree(vector<int>(ino.begin(), it),vector<int>(posto.begin(),posto.begin()+i));
TreeNode*right=buildTree(vector<int>(it+1,ino.end()),vector<int>(posto.begin()+i,posto.end()-1));
return new TreeNode(l, left, right);
}
};
```
## 105. 从前序与中序遍历序列构造二叉树
```cpp
class Solution {
public:
TreeNode* buildTree(vector<int> preorder, vector<int>ino) {
if(ino.empty())return NULL;
int l =preorder.front();
auto it=find(ino.begin(),ino.end(),l);
int i =distance(ino.begin(),it);
TreeNode*left=buildTree(vector<int>(preorder.begin()+1,preorder.begin()+i+1),vector<int>(ino.begin(), it));
TreeNode*right=buildTree(vector<int>(preorder.begin()+i+1,preorder.end()),vector<int>(it+1,ino.end()));
return new TreeNode(l,left,right);
}
};
```

## 学习感想
41 changes: 41 additions & 0 deletions notes/src/day18/lc112.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 112. 路径总和

## 题目描述

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

## 解题思路

```cpp
class Solution {
public:
bool hasPathSum(TreeNode*p,int t) {
if(!p)return false;if(!p->left&&!p->right)return p->val==t;return hasPathSum(p->left,t-p->val)||hasPathSum(p->right,t-p->val);
}
};
```
## 113. 路径总和 II
## 题目描述
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
## 解题思路
```cpp
class Solution {
public:
void f(vector<int>&path,TreeNode*p,vector<vector<int>>&res,int t){if(!p)return;path.push_back(p->val);
if(!p->left&&!p->right)if(t==p->val)res.push_back(path);f(path,p->left,res,t-p->val);f(path,p->right,res,t-p->val);path.pop_back();}
vector<vector<int>> pathSum(TreeNode*p,int t) {
vector<vector<int>> res;vector<int> path;f(path,p,res,t);return res;
}
};
```
## 学习感想
52 changes: 52 additions & 0 deletions notes/src/day18/lc513.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 513. 找树左下角的值

## 题目描述

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

## 解题思路

迭代法简单

```cpp
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
deque<TreeNode* > v;
int res=0;
v.push_back(root);
while (!v.empty()) {
res=v.front()->val;
int n = v.size();
for (int i = 0; i < n;i++) {
TreeNode* ptr=v.front();v.pop_front();
if(ptr->left)v.push_back(ptr->left);
if(ptr->right)v.push_back(ptr->right);
}
}
return res;
}
};
```
递归法
```cpp
class Solution {
public:
void f(TreeNode*p,int d,int&res,int&resd) {
if(!p)return;
if(d>resd){resd=d;res=p->val;}
f(p->left,d+1,res,resd);
f(p->right,d+1,res,resd);
}
int findBottomLeftValue(TreeNode*root) {
int res=0,resd=0;
f(root,1,res,resd);
return res;
}
};
```
## 学习感想

0 comments on commit 12fc132

Please sign in to comment.