方法:递归
class Solution {
private:
int res;
int maxn = INT_MIN;
void traversal(TreeNode* cur, int dep) {
if (cur->left == NULL && cur->right == NULL) {
if (maxn < dep) {
maxn = dep;
res = cur->val;
}
return ;
}
if (cur->left) traversal(cur->left, dep + 1);
if (cur->right) traversal(cur->right, dep + 1);
return ;
}
public:
int findBottomLeftValue(TreeNode* root) {
if (root == NULL) return 0;
traversal(root, 0);
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:迭代
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*>q;
int val;
if(root != NULL) q.push(root);
while(!q.empty()) {
int n = q.size();
for(int i = 0; i < n; ++i) {
TreeNode* nod = q.front();
q.pop();
if(i == 0) val = nod->val;
if(nod->left) q.push(nod->left);
if(nod->right) q.push(nod->right);
}
}
return val;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:迭代
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
stack<pair<TreeNode*, int>> stk;
if (root == NULL) return false;
stk.push(pair<TreeNode*, int>(root, root->val));
while (!stk.empty()) {
pair<TreeNode*, int> nod = stk.top();
stk.pop();
if (!nod.first->right && !nod.first->left && sum == nod.second) return true;
if (nod.first->right) stk.push(pair<TreeNode*, int>(nod.first->right, nod.second + nod.first->right->val));
if (nod.first->left) stk.push(pair<TreeNode*, int>(nod.first->left, nod.second + nod.first->left->val));
}
return false;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:递归
class Solution {
private:
bool traversal(TreeNode* cur, int sum) {
if (!cur->left && !cur->right && sum == 0) return true;
if (!cur->left && !cur->right) return false;
if (cur->left) {
if (traversal(cur->left, sum - cur->left->val)) return true;
}
if (cur->right) {
if (traversal(cur->right, sum - cur->right->val)) return true;
}
return false;
}
public:
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
return traversal(root, sum - root->val);
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:回溯
class Solution {
private:
vector<vector<int>> res;
vector<int> path;
void traversal(TreeNode* cur, int sum) {
if (!cur->left && !cur->right && sum == 0) {
res.emplace_back(path);
return;
}
if (!cur->left && !cur->right) return;
if (cur->left) {
path.emplace_back(cur->left->val);
traversal(cur->left, sum - cur->left->val);
path.pop_back();
}
if (cur->right) {
path.emplace_back(cur->right->val);
traversal(cur->right, sum - cur->right->val);
path.pop_back();
}
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
res.clear();
path.clear();
if (root == NULL) return res;
path.emplace_back(root->val);
traversal(root, sum - root->val);
return res;
}
};
$时间复杂度O(),空间复杂度O(n)
方法:递归
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(n),空间复杂度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>& preorder, int pb, int pe, vector<int>& inorder, int ib, int ie) {
if (pb == pe) return NULL;
int val = preorder[pb];
TreeNode* root = new TreeNode(val);
if (pe - pb == 1) return root;
int k;
for (k = 0; k < inorder.size(); ++k) if (inorder[k] == val) break;
int lib = ib, lie = k, rib = k + 1, rie = ie;
int lpb = pb + 1, lpe = pb + 1 + k - lib, rpb = pb + 1 + k - lib, rpe = pe;
root->left = construct_binary_tree(preorder, lpb, lpe, inorder, lib, lie);
root->right = construct_binary_tree(preorder, rpb, rpe, inorder, rib, rie);
return root;
}
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (!preorder.size() || !inorder.size()) return NULL;
return construct_binary_tree(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
};
$时间复杂度O(n),空间复杂度O(n)