LeetCode 110 平衡二叉树
题目链接:110
思路:判断是否平衡,即判断每个节点的左右子树高度差是否小于等于1。因此,可采用后序遍历的方式,先求左右孩子高度,若左右孩子高度差小于等于1,则返回当前节点高度,否则当前子树不平衡,返回-1。另外,若左右孩子至少一个高度为-1,说明已经不平衡,无需再计算高度差,直接返回-1。
代码:
/**
* 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 getHeight(TreeNode* root) {
if (!root) return 0;
int left_height = getHeight(root->left);
if (left_height == -1) return -1;
int right_height = getHeight(root->right);
if (right_height == -1) return -1;
return abs(left_height-right_height)>1 ? -1 : max(left_height, right_height) + 1;
}
bool isBalanced(TreeNode* root) {
return getHeight(root) != -1;
}
};
LeetCode 257 二叉树的所有路径
题目链接:257
递归
思路:路径顺序自上而下,因此采用前序遍历。函数参数需传入当前节点、之前的路径和保存结果的数组。若遍历到叶子节点,说明已遍历完一条路径,即可将结果保存并返回。在单层递归中,先将当前节点加入路径,然后向左右孩子进行递归以寻找叶子节点。
代码:
/**
* 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:
void getPath(TreeNode* root, string path, vector<string>& paths) {
path += to_string(root->val);
if (!root->left && !root->right) {
paths.push_back(path);
return;
}
if (root->left) getPath(root->left, path+"->", paths);
if (root->right) getPath(root->right, path+"->", paths);
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if (!root) return result;
getPath(root, "", result);
return result;
}
};
迭代
思路:用栈完成前序遍历,一个栈保存节点,另一个栈保存对应节点的路径。对节点的操作为判断是否为叶子节点,若是则保存路径。
代码:
/**
* 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) {
vector<string> result;
stack<TreeNode*> tree_stack;
stack<string> path_stack;
if (root) {
tree_stack.push(root);
path_stack.push(to_string(root->val));
}
while (!tree_stack.empty()) {
TreeNode* cur = tree_stack.top();
tree_stack.pop();
string path = path_stack.top();
path_stack.pop();
if (!cur->left && !cur->right) {
result.push_back(path);
}
if (cur->right) {
tree_stack.push(cur->right);
path_stack.push(path+"->"+to_string(cur->right->val));
}
if (cur->left) {
tree_stack.push(cur->left);
path_stack.push(path+"->"+to_string(cur->left->val));
}
}
return result;
}
};
LeetCode 404 左叶子之和
题目链接:404
递归
思路:采用后序遍历,统计左右子树的左叶子之和。叶子节点可通过左右孩子是否都为NULL
判断;为判断是否是左叶子,可将左、右作为参数传给下层。
代码:
/**
* 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 recurse(TreeNode* root, bool left) {
if (!root) return 0;
if (!root->left && !root->right) {
if (left) return root->val;
else return 0;
}
return recurse(root->left, 1) + recurse(root->right, 0);
}
int sumOfLeftLeaves(TreeNode* root) {
return recurse(root, 0);
}
};
迭代
思路:遍历节点,并判断每个节点的左孩子是否是叶子节点,若是则将值累加。
代码:
/**
* 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) {
queue<TreeNode*> que;
int result = 0;
if (root) que.push(root);
while (!que.empty()) {
TreeNode* cur = que.front();
que.pop();
if (cur->left) {
if (!cur->left->left && !cur->left->right) result += cur->left->val;
else que.push(cur->left);
}
if (cur->right) que.push(cur->right);
}
return result;
}
};