方法:递归
/**
* 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 {
int getDep(TreeNode* cur) {
if (cur == NULL) return 0;
int l = getDep(cur->left);
if (l == -1) return -1;
int r = getDep(cur->right);
if (r == -1) return -1;
return abs(l-r) > 1 ? -1 : max(l, r) + 1;
}
public:
bool isBalanced(TreeNode* root) {
if (getDep(root) == -1) return false;
return true;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:递归
private:
void solve(TreeNode* cur, string path, vector<string>& res) {
path += to_string(cur->val);
if (!cur->left && !cur->right) {
res.push_back(path);
return ;
}
if (cur->left) solve(cur->left, path + "->", res);
if (cur->right) solve(cur->right, path + "->", res);
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string path;
solve(root, path, res);
return res;
}
};
$时间复杂度O(),空间复杂度O(
)
方法:迭代
/**
* 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:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> nod;
stack<string> str;
vector<string> res;
if (root == NULL) return res;
nod.push(root);
str.push(to_string(root->val));
while(!nod.empty()) {
TreeNode* cur = nod.top(); nod.pop();
string path = str.top(); str.pop();
if (cur->left == NULL && cur->right == NULL) {
res.emplace_back(path);
}
if (cur->right) {
nod.push(cur->right);
str.push(path + "->" + to_string(cur->right->val));
}
if (cur->left) {
nod.push(cur->left);
str.push(path + "->" + to_string(cur->left->val));
}
}
return res;
}
};
$时间复杂度O(),空间复杂度O(
)
方法:递归
/**
* 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 sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;
int l = sumOfLeftLeaves(root->left);
int r = sumOfLeftLeaves(root->right);
int midv = 0;
if (root->left && !root->left->left && !root->left->right) midv += root->left->val;
int res = l + r + midv;
return res;
}
};
$时间复杂度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 {
public:
int sumOfLeftLeaves(TreeNode* root) {
stack<TreeNode*> stk;
if (root != NULL) stk.push(root);
int res = 0;
while (!stk.empty()) {
TreeNode* nod = stk.top(); stk.pop();
if (nod->left != NULL && !nod->left->left && !nod->left->right) res += nod->left->val;
if (nod->right) stk.push(nod->right);
if (nod->left) stk.push(nod->left);
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)