Commit f0c74ea7 by 李楚霏

week3

parent fcc8c9ce
// 解题思路:自顶向下层次遍历后(bfs)再用stl的reverse函数对结果函数进行翻转
// 就能得到自底向上的层次遍历
#include<iostream>
#include <vector>
#include<queue>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
auto qu=queue<TreeNode*>();
auto res = vector<vector<int>>();
if(!root) return res;
qu.push(root);
while(!qu.empty()) {
auto size = qu.size();
auto tmp = vector<int>();
for(int i =0; i < size; i++){
auto ans = qu.front();
qu.pop();
tmp.push_back(ans->val);
if(ans->left) {
qu.push(ans->left);
}
if(ans->right) {
qu.push(ans->right);
}
}
res.push_back(tmp);
}
reverse(res.begin(), res.end());
return res;
}
};
\ No newline at end of file
// 解题思路:
// 使用bfs的思路: 队列负责收入每层的节点,当前层的节点都执行到就pop,
// 然后再push下一层的节点
#include <iostream>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
vector<double> averageOfLevels(TreeNode *root)
{
auto averages = vector<double>();
auto qu = queue<TreeNode *>();
qu.push(root);
while (!qu.empty())
{
double sum = 0;
int size = qu.size();
for (int i = 0; i < size; i++)
{
auto node = qu.front();
qu.pop();
sum += node->val;
if (node->left != nullptr)
{
qu.push(node->left);
}
if (node->right != nullptr)
{
qu.push(node->right);
}
}
averages.push_back(sum / size);
}
return averages;
}
};
int main()
{
return 0;
}
\ No newline at end of file
/**解题思路:
* 使用dfs遍历,遍历一个节点就用map存储它的父节点,
* p和q从下往上遍历,q只要访问到已被访问过的节点,那么这个节点就相当于p和q的最大公共祖先
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
let fa = [];
let vis = [];
const dfs = (root) => {
if(root.left) {
fa[root.left.val] = root;
dfs(root.left);
}
if(root.right) {
fa[root.right.val] = root;
dfs(root.right);
}
}
fa[root.val] = null;
dfs(root);
while(p) {
vis[p.val] = true;
p = fa[p.val];
}
while(q) {
if(vis[q.val]) return q;
q = fa[q.val];
}
return null;
};
/**解题思路:组合是回溯的经典题目;回溯主要是用来解决找出所有方案的。
* 它的优化方案是剪枝,
* @param {number} n
* @param {number} k
* @return {number[][]}
*/
var combine = function(n, k) {
//termiatior
let res =[];
if (n === 0 || n<k){
return res;
}
const helper = (start, path) => {
if (path.length === k) {
// 如果走到分支尽头,就把完整解送入res
// slice()是浅拷贝。返回一个新的拷贝数组;
// 但原数组不会受到影响
res.push(path.slice());
return;
}
// 枚举
for(let i = start; i <= n; i++) {
path.push(i);
// 这步相当于剪枝,下一个搜索起点为上一个搜索起点+1
helper(i+1, path);
path.pop();
}
}
//drillDown
helper(1, []);
return res;
};
\ No newline at end of file
var permute = function(nums) {
let path = [];
let res =[];
let used = {};
const permuteHelper = (path) => {
if(path.length === nums.length) {
console.error(`回溯完成的path:${path}`);
res.push(path.slice());
}
// for(let i = 0; i < nums.length; i++){
// if(nums[start] === path[i]) continue;
// path.push(nums[i]);
// permuteHelper(i+1, path);
// }
//没有一次编译过的问题,变量没有加声明符号
for(const num of nums) {
if(used[num]) continue;
console.error(`开始回溯的path: ${path}`);
path.push(num);
used[num] = true;
permuteHelper(path);
path.pop();
console.error(`回溯到上一层的path: ${path}`);
used[num] = false;
console.error(`是否遍历过: ${used[num]}`);
}
}
permuteHelper(path);
return res;
};
let nums = [1,2,3];
permute(nums);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment