LeetCode513.找树左下角的值
/**
* 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:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
que.push(root);
int first;
while(!que.empty()){
int size = que.size();
for(int i=0;i<size;i++){
TreeNode* tmp = que.front();
que.pop();
if(i==0) first = tmp->val;
if(tmp->left) que.push(tmp->left);
if(tmp->right) que.push(tmp->right);
}
}
return first;
}
};
112. 路径总和
/**
* 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:
bool backtracking(const int targetSum,TreeNode* root,int sum){
if(root->left==NULL&&root->right==NULL&&targetSum==sum)
return true;
//if(!root->left&&!root->right) return false;
if(root->left){
sum+=root->left->val;
if(backtracking(targetSum,root->left,sum)) return true;
sum-=root->left->val;
}
if(root->right){
sum+=root->right->val;
if(backtracking(targetSum,root->right,sum)) return true;
sum-=root->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==NULL)return false;
int sum =root->val;
bool flag = backtracking(targetSum,root,sum);
return flag;
}
};
106.从中序与后序遍历序列构造二叉树
/**
* 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:
TreeNode* traversal(vector<int>& inorder,vector<int>& postorder){
if(postorder.size()==0) return NULL;
int rootvalue = postorder[postorder.size()-1];
TreeNode* root = new TreeNode(rootvalue);
if(postorder.size()==1)return root;
int delimiterindex;
for(delimiterindex=0;delimiterindex<inorder.size();delimiterindex++){
if(inorder[delimiterindex]==rootvalue) break;
}
vector<int> leftinorder(inorder.begin(),inorder.begin()+delimiterindex);
vector<int> rightinorder(inorder.begin()+delimiterindex+1,inorder.end());
postorder.resize(postorder.size()-1);
vector<int> leftpostorder(postorder.begin(),postorder.begin()+leftinorder.size());
vector<int> rightpostorder(postorder.begin()+leftinorder.size(),postorder.end());
root->left = traversal(leftinorder,leftpostorder);
root->right = traversal(rightinorder,rightpostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==0||postorder.size()==0) return NULL;
return traversal(inorder,postorder);
}
};