513.找树左下角的值
class Solution {
public:
int findBottomLeftValue(TreeNode* root)
{
queue<TreeNode*>que;
if(root!=nullptr)
que.push(root);
int res=0;
while(!que.empty())
{
int size=que.size();
for(int i=0;i<size;i++)
{
TreeNode*temp=que.front();
que.pop();
if(i==0)
res=temp->val;
if(temp->left)
que.push(temp->left);
if(temp->right)
que.push(temp->right);
}
}
return res;
}
};
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth) {
if (root->left == NULL && root->right == NULL) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
return;
}
if (root->left) {
depth++;
traversal(root->left, depth);
depth--; // 回溯
}
if (root->right) {
depth++;
traversal(root->right, depth);
depth--; // 回溯
}
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return result;
}
};
112. 路径总和 113.路径总和ii
112. 路径总和
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum)
{
if(root==nullptr)
return false;
return traversal(root,targetSum-root->val);
}
bool traversal(TreeNode*cur,int count)
{
if(!cur->left&&!cur->right&&count==0)
return true;
if(!cur->left&&!cur->right)
return false;
if(cur->left)
{
count-=cur->left->val;
if(traversal(cur->left,count))
return true;
count+=cur->left->val;
}
if (cur->right) { // 右
count -= cur->right->val; // 递归,处理节点;
if (traversal(cur->right, count)) return true;
count += cur->right->val; // 回溯,撤销处理结果
}
return false;
}
};
class solution {
public:
bool haspathsum(TreeNode* root, int sum) {
if (root == null) return false;
// 此时栈里要放的是pair<节点指针,路径数值>
stack<pair<TreeNode*, int>> st;
st.push(pair<TreeNode*, int>(root, root->val));
while (!st.empty()) {
pair<TreeNode*, int> node = st.top();
st.pop();
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
if (!node.first->left && !node.first->right && sum == node.second) return true;
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if (node.first->right) {
st.push(pair<TreeNode*, int>(node.first->right, node.second + node.first->right->val));
}
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if (node.first->left) {
st.push(pair<TreeNode*, int>(node.first->left, node.second + node.first->left->val));
}
}
return false;
}
};
class Solution {
public:
vector<vector<int>>res;
vector<int>path;
vector<vector<int>> pathSum(TreeNode* root, int targetSum)
{
res.clear();
path.clear();
if(root==nullptr)
return res;
path.push_back(root->val);
dfs(root,targetSum-root->val);
return res;
}
void dfs(TreeNode*root,int targetSum)
{
if(!root->left&&!root->right&&targetSum==0)
{
res.push_back(path);
return;
}
if(!root->left&&!cur->right)
return;
if(root->left)
{
path.push_back(root->left);
targetSum-=root->left->val;
dfs(root->left,targetSum);
targetSum+=root->left->val;
path.pop_back();
}
if(root->right)
{
path.push_back(root->right);
targetSum-=root->right->val;
dfs(root->right,targetSum);
targetSum+=root->right->val;
path.pop_back();
}
return;
}
// vector<vector<int>>res;
// vector<int>path; dfs(root,targetSum,path);
// return res;
// }
// void dfs(TreeNode*root,int targetSum,vector<int>path)
// {
// if(root==nullptr)
// return;
// targetSum-=root->val;
// path.push_back(root->val);
// if(targetSum==0&&!root->left&&!root->right)
// {
// res.push_back(path);
// return;
// }
// dfs(root->left,targetSum,path);
// dfs(root->right,targetSum,path);
// }
};
106. 从中序与后序遍历序列构造二叉树
106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
public:
unordered_map<int,int>mp;
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
for(int i=0;i<inorder.size();i++)
{
mp[inorder[i]]=i;
}
return dfs(inorder,postorder,0,inorder.size()-1,0,inorder.size()-1);
}
TreeNode* dfs(vector<int>& inorder, vector<int>& postorder,int inorder_left,int inorder_right,int postorder_left,int postorder_right)
{
if(inorder_left>inorder_right||postorder_left>postorder_right)
return nullptr;
int postorder_root=postorder[postorder_right];
int inorder_root=mp[postorder_root];
TreeNode*root=new TreeNode(postorder_root);
int size_left_tree=inorder_root-inorder_left;
root->left=dfs(inorder,postorder,inorder_left,inorder_root-1,postorder_left,size_left_tree+postorder_left-1);
root->right=dfs(inorder,postorder,inorder_root+1,inorder_right,size_left_tree+postorder_left,postorder_right-1);
return root;
}
// int n=inorder.size();
// for(int i=0;i<n;i++)
// {
// mp[inorder[i]]=i;
// }
// return build(inorder,postorder,0,n-1,0,n-1);
// }
// TreeNode*build(vector<int>& inorder, vector<int>& postorder,int inorder_left,int inorder_right,int postorder_left,int postorder_right)
// {
// if(inorder_left>inorder_right||postorder_left>postorder_right)
// return nullptr;
// int postorder_root=postorder_right;
// int inorder_root=mp[postorder[postorder_root]];
// TreeNode*root=new TreeNode(postorder[postorder_root]);
// int size_left_tree=inorder_root-inorder_left;
// root->left=build(inorder,postorder,inorder_left,inorder_root-1,postorder_left,postorder_left+size_left_tree-1);
// root->right=build(inorder,postorder,inorder_root+1,inorder_right,postorder_left+size_left_tree,postorder_right-1);
// return root;
// }
};
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
private:
unordered_map<int, int> index;
public:
TreeNode* myBuildTree(const vector<int>& preorder, const vector<int>& inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
if (preorder_left > preorder_right) {
return nullptr;
}
// 前序遍历中的第一个节点就是根节点
int preorder_root = preorder_left;
// 在中序遍历中定位根节点
int inorder_root = index[preorder[preorder_root]];
// 先把根节点建立出来
TreeNode* root = new TreeNode(preorder[preorder_root]);
// 得到左子树中的节点数目
int size_left_subtree = inorder_root - inorder_left;
// 递归地构造左子树,并连接到根节点
// 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素
root->left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1);
// 递归地构造右子树,并连接到根节点
// 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素
root->right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
// 构造哈希映射,帮助我们快速定位根节点
for (int i = 0; i < n; ++i) {
index[inorder[i]] = i;
}
return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
}
};