513
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
int res;
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (i == 0) res = node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return res;
}
};
递归法
class Solution {
public:
void dfs(TreeNode* cur,int height, int& curHeight,int& res ){
if(cur == NULL){
return;
}
height++;
dfs(cur->left,height,curHeight,res);
dfs(cur->right,height,curHeight,res);
if(height > curHeight){
curHeight = height;
res = cur->val;
}
}
int findBottomLeftValue(TreeNode* root) {
int res, curHeight = 0;
dfs(root, 0, curHeight,res);
return res;
}
};
112
class Solution {
public:
void travel(TreeNode* cur,vector<int>& path,int targetSum, bool& res){
path.push_back(cur->val);
if(cur->left == NULL && cur->right == NULL){
int total = accumulate(path.begin(), path.end(), 0);
if(total == targetSum){
res = true;
return;
}
}
if(cur->left){
travel(cur->left,path,targetSum,res);
path.pop_back();
}
if(cur->right){
travel(cur->right,path,targetSum,res);
path.pop_back();
}
}
bool hasPathSum(TreeNode* root, int targetSum) {
bool res = false;
vector<int> path;
if(root == NULL) return res;
travel(root,path,targetSum,res);
return res;
}
};
113 路经总和
class Solution {
public:
void travel(TreeNode* cur,vector<int>& path,int targetSum,vector<vector<int>>& res ){
path.push_back(cur->val);
if(cur->left == NULL && cur->right == NULL){
int total = accumulate(path.begin(), path.end(), 0);
if(total == targetSum){
res.push_back(path);
return;
}
}
if(cur->left){
travel(cur->left,path,targetSum,res);
path.pop_back();
}
if(cur->right){
travel(cur->right,path,targetSum,res);
path.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> res ;
vector<int> path;
if(root == NULL) return res;
travel(root,path,targetSum,res);
return res;
}
};
106
class Solution {
private:
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;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder, postorder);
}
};