day 17 记录代码随想录
第一题 力扣110 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过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* node) {
if (node == NULL) return 0;
int l = getHeight(node->left);
if (l == -1) return -1;
int r = getHeight(node->right);
if (r == -1) return -1;
return abs(r - l) > 1 ? -1 : 1 + max(r, l);
}
bool isBalanced(TreeNode* root) { return getHeight(root) == -1 ? 0 : 1; }
};
第二题 力扣257.二叉树的所有路径
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
题目链接:力扣题目链接
二叉树的递归回溯算法,前序遍历:
/**
* 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 traversal(TreeNode* node, vector<int>& path, vector<string>& result) {
path.push_back(node->val); //中
if(node->left == NULL && node->right == NULL) {
string spath;
for (int i = 0; i < path.size()-1; i++) {
spath += to_string(path[i]);
spath += "->";
}
spath += to_string(path[path.size()-1]);
result.push_back(spath);
}
if(node->left) {
traversal(node->left, path, result); //左
path.pop_back(); //回溯
}
if(node->right) {
traversal(node->right, path, result); //右
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
to_string()强制类型转换
迭代法:
/**
* 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;
if(root == NULL) return result;
stack<TreeNode*> st;
stack<string> pathst;
st.push(root);
pathst.push(to_string(root->val));
while(!st.empty()) {
TreeNode* node = st.top();
st.pop();
string path = pathst.top();
pathst.pop();
if(node->left == NULL && node->right == NULL) {
result.push_back(path);
}
if(node->left) {
st.push(node->left);
pathst.push(path + "->" + to_string(node->left->val));
}
if(node->right) {
st.push(node->right);
pathst.push(path + "->" + to_string(node->right->val));
}
}
return result;
}
};
第三题 力扣404. 左叶子之和
题目链接:力扣题目链接
递归法:
/**
* 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 findLeft(TreeNode* node, int &sum) {
if(node->left != NULL && node->left->left == NULL && node->left->right == NULL) {
sum += node->left->val;
}
if(node->left) {
findLeft(node->left,sum);
}
if(node->right) {
findLeft(node->right,sum);
}
}
int sumOfLeftLeaves(TreeNode* root) {
int sum = 0;
if(root == NULL) return sum;
findLeft(root,sum);
return sum;
}
};