方法:递归
/**
* 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:
int maxv = INT_MIN;
int val = 0;
void solve(TreeNode* cur, int dep) {
if (!cur->left && !cur->right) {
if (maxv < dep) {
maxv = dep;
val = cur->val;
}
return ;
}
if (cur->left) solve(cur->left, dep+1);
if (cur->right) solve(cur->right, dep+1);
}
public:
int findBottomLeftValue(TreeNode* root) {
solve(root, 1);
return val;
}
};
$时间复杂度O(n),空间复杂度O(h)h为树的高度
方法:bfs
/**
* 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*> q;
q.push(root);
int val;
while (!q.empty()) {
int n = q.size();
for (int i = 0; i < n; ++i) {
TreeNode* nod = q.front();
q.pop();
if (!i) val = nod->val;
if (nod->left) q.push(nod->left);
if (nod->right) q.push(nod->right);
}
}
return val;
}
};
$时间复杂度O(n),空间复杂度O(h)h为树的高度
方法:递归
/**
* 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:
bool solve(TreeNode* cur, int mut) {
if (!cur->left && !cur->right && !mut) return true;
if (!cur->left && !cur->right) return false;
if (cur->left) if (solve(cur->left, mut-cur->left->val)) return true;
if (cur->right) if (solve(cur->right, mut-cur->right->val)) return true;
return false;
}
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == NULL) return false;
return solve(root, targetSum-root->val);
}
};
$时间复杂度O(n),空间复杂度O(h)h为树的高度
方法:递归
/**
* 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:
vector<vector<int>> res;
vector<int> path;
void solve(TreeNode* cur, int mut) {
if (!cur->left && !cur->right && !mut) {
res.push_back(path);
}
if (cur->left) {
path.push_back(cur->left->val);
solve(cur->left, mut-cur->left->val);
path.pop_back();
}
if (cur->right) {
path.push_back(cur->right->val);
solve(cur->right, mut-cur->right->val);
path.pop_back();
}
}
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
res.clear();
path.clear();
if (root == NULL) return res;
path.push_back(root->val);
solve(root, targetSum-root->val);
return res;
}
};
$时间复杂度O(),空间复杂度O(n)
方法:分治+递归
/**
* 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:
TreeNode* construct_binary_tree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe) {
if (pb == pe) return NULL;
int val = postorder[pe-1];
TreeNode* root = new TreeNode(val);
if (pe - pb == 1) return root;
int k;
for (k = 0; k < ie; ++k) {
if (inorder[k] == val) break;
}
int lib = ib, lie = k, rib = k + 1, rie = ie;
int lpb = pb, lpe = k + pb - ib, rpb = k + pb - ib, rpe = pe - 1;
root->left = construct_binary_tree(inorder, lib, lie, postorder, lpb, lpe);
root->right = construct_binary_tree(inorder, rib, rie, postorder, rpb, rpe);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return construct_binary_tree(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};
$时间复杂度O(),空间复杂度O(h)h为树的高度
方法:分治+递归
/**
* 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:
TreeNode* construct_binary_tree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe) {
if (pb == pe) return NULL;
int val = postorder[pe-1];
TreeNode* root = new TreeNode(val);
if (pe - pb == 1) return root;
int k;
for (k = 0; k < ie; ++k) {
if (inorder[k] == val) break;
}
int lib = ib, lie = k, rib = k + 1, rie = ie;
int lpb = pb, lpe = k + pb - ib, rpb = k + pb - ib, rpe = pe - 1;
root->left = construct_binary_tree(inorder, lib, lie, postorder, lpb, lpe);
root->right = construct_binary_tree(inorder, rib, rie, postorder, rpb, rpe);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return construct_binary_tree(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};
$时间复杂度O(),空间复杂度O(h)h为树的高度