基本概念
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。
方法:递归
class Solution {
private:
int getDep(TreeNode* cur) {
if (cur == NULL) return 0;
int lDep = getDep(cur->left); //(左)
if (lDep == -1) return -1;
int rDep = getDep(cur->right); //(右)
if (rDep == -1) return -1;
return abs(lDep-rDep) > 1 ? -1 : 1 + max(lDep, rDep);
}
public:
bool isBalanced(TreeNode* root) {
return getDep(root) == -1 ? false : true; //(中)
}
};
$时间复杂度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:
int getDep(TreeNode* root) {
stack<TreeNode*> stk;
if (root == NULL) return 0;
stk.push(root);
int dep = 0, res = 0;
while (!stk.empty()) {
TreeNode* nod = stk.top();
if (nod != NULL) {
stk.pop();
stk.push(nod);
stk.push(NULL);
dep++;
if (nod->right) stk.push(nod->right);
if (nod->left) stk.push(nod->left);
} else {
stk.pop();
nod = stk.top();
stk.pop();
dep--;
}
res = res > dep ? res : dep;
}
return res;
}
public:
bool isBalanced(TreeNode* root) {
if (root == NULL) return true;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* nod = st.top();
st.pop();
if (abs(getDep(nod->right) - getDep(nod->left)) > 1) return false;
if (nod->left) st.push(nod->left);
if (nod->right) st.push(nod->right);
}
return true;
}
};
$时间复杂度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 {
void traversal(vector<string>& res, vector<int>& path, TreeNode* cur) {
path.emplace_back(cur->val);
if(cur->left == NULL && cur->right == NULL) {
int n = path.size();
string str;
for(int i = 0; i < n-1; ++i) {
str += to_string(path[i]);
str += "->";
}
str += to_string(path[n-1]);
res.emplace_back(str);
return;
}
if(cur->left) {
traversal(res, path, cur->left);
path.pop_back();
}
if(cur->right) {
traversal(res, path, cur->right);
path.pop_back();
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string>res;
vector<int>path;
if(root == NULL) return res;
traversal(res, path, root);
return res;
}
};
$时间复杂度O(),空间复杂度O()
精简版
class Solution {
private:
void traversal(TreeNode* cur, vector<string>& res, string path) {
path += to_string(cur->val);
if (cur->left == NULL && cur->right == NULL) {
res.emplace_back(path);
return;
}
if (cur->left) traversal(cur->left, res, path + "->");
if (cur->right) traversal(cur->right, res, path + "->");
return;
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string path;
if (root == NULL) return res;
traversal(root, res, path);
return res;
}
};
$时间复杂度O(),空间复杂度O()
方法:迭代
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()
方法:递归
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root == NULL) return 0;
int lval = sumOfLeftLeaves(root->left);
int rval = sumOfLeftLeaves(root->right);
int midv = 0;
if(root->left && !root->left->left && !root->left->right) midv += root->left->val;
int res = lval + rval + midv;
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法:迭代
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
stack<TreeNode*>st;
int res = 0;
if (root == NULL) return res;
st.push(root);
while (!st.empty()) {
TreeNode* nod = st.top(); st.pop();
if (nod->left != NULL && !nod->left->left && !nod->left->right) {
res += nod->left->val;
}
if (nod->right) st.push(nod->right);
if (nod->left) st.push(nod->left);
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)