第一题是平衡二叉树https://leetcode.cn/problems/balanced-binary-tree/,初步思路是利用递归算node->left与node->right的差。卡在了如何表示node->left与node->right之差。递归返回值记录左右子树的平衡情况,平衡return height,失衡return -1,终止条件为node == NULL,return 0,单层递归的逻辑为判断左右子树高度之差,>1 return -1,否则return高度。
class Solution {
public:
int getheight(TreeNode* node) {
if (node == NULL)
return 0;
int leftheight = getheight(node->left);
if (leftheight == -1)
return -1;
int rightheight = getheight(node->right);
if (rightheight == -1)
return -1;
if (abs(leftheight - rightheight) > 1)
return -1;
else
return max(leftheight, rightheight) + 1;
}
bool isBalanced(TreeNode* root) {
int result = getheight(root);
if (result == -1)
return false;
else
return true;
}
};
另外一种方法是迭代法。
class Solution {
private:
int getDepth(TreeNode* cur) {
stack<TreeNode*> st;
if (cur != NULL)
st.push(cur);
int depth = 0;
int result = 0;
while (!st.empty()) {
TreeNode* node = st.top();
if (node != NULL) {
st.pop();
st.push(node);
st.push(NULL);
depth++;
if (node->right)
st.push(node->right);
if (node->left)
st.push(node->left);
} else {
st.pop();
node = st.top();
st.pop();
depth--;
}
result = result > depth ? result : depth;
}
return result;
}
public:
bool isBalanced(TreeNode* root) {
stack<TreeNode*> st;
if (root == NULL)
return true;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
if (abs(getDepth(node->left) - getDepth(node->right)) > 1)
return false;
if (node->right)
st.push(node->right);
if (node->left)
st.push(node->left);
}
return true;
}
};
第二题是二叉树的所有路径https://leetcode.cn/problems/binary-tree-paths/,由于此题需要寻找父节点到叶子结点的路径,应此应先处理中间结点,用前序遍历的方式是最合适的。
class Solution {
public:
void travelsal(TreeNode* cur, vector<int>& path, vector<string>& result) {
path.push_back(cur->val);
if (cur->left == NULL && cur->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);
return;
}
if (cur->left) {
travelsal(cur->left, path, result);
path.pop_back();
}
if (cur->right) {
travelsal(cur->right, path, result);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> path;
if (root == NULL)
return result;
travelsal(root, path, result);
return result;
}
};
下面是迭代法的代码。
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> treeSt;
stack<string> pathSt;
vector<string> result;
if (root == NULL)
return result;
treeSt.push(root);
pathSt.push(to_string(root->val));
while (!treeSt.empty()) {
TreeNode* node = treeSt.top();
treeSt.pop();
string path = pathSt.top();
pathSt.pop();
if (node->left == NULL && node->right == NULL) {
result.push_back(path);
}
if (node->right) {
treeSt.push(node->right);
pathSt.push(path + "->" + to_string(node->right->val));
}
if (node->left) {
treeSt.push(node->left);
pathSt.push(path + "->" + to_string(node->left->val));
}
}
return result;
}
};
第三题是左叶子之和https://leetcode.cn/problems/sum-of-left-leaves/description/,递归代码如下:
class Solution {
public:
int travel(TreeNode* node) {//后序遍历
if (node == NULL)
return 0;
if (node->left == NULL && node->right == NULL)
return 0;
int leftnum = travel(node->left);
if (node->left != NULL && node->left->left == NULL &&
node->left->right == NULL) {
leftnum = node->left->val;
}
int rightnum = travel(node->right);
return leftnum + rightnum;
}
int sumOfLeftLeaves(TreeNode* root) { return travel(root); }
};
已经第17天了,加油加油!