参考
题目一:LeetCode 110.平衡二叉树
递归法
- 确定递归函数的参数和返回值:参数为根节点,返回值是以当前传入节点为根节点的树的高度。
int getHeight(TreeNode* root);
- 确定终止条件:当传入的节点的为空时,返回0表示节点高度为0
if(root == nullptr) return 0;
- 确定单层递归逻辑:单层逻辑里肯定是求左子树高度和右子树高度的高度差,如果高度差大于1,说明已经不满足条件,返回-1,否则直接返回高度差
int leftHeight = getHeight(root->left);
if(leftHeight == -1) return -1;
int rightHeight = getHeight(root->right);
if(rightHeight == -1) return -1;
return abs(leftHeight-rightHeight)>1?-1:(1+max(leftHeight,rightHeight));
完整的代码如下:
/**
* 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 == nullptr) return 0;
int leftHeight = getHeight(root->left);
if(leftHeight == -1) return -1;
int rightHeight = getHeight(root->right);
if(rightHeight == -1) return -1;
return abs(leftHeight-rightHeight)>1?-1:(1+max(leftHeight,rightHeight));
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
在看代码的时候理解不了,原因在于自己还是没有理解概念。
- 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。
- 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。
总的来说,上面的代码思路就是去求每一个节点作为根节点时其左右子树的高度,如果高度差大于1,则返回-1作标记,否则返回当前节点的高度。
迭代法
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;
}
};
题目二:LeetCode 257.二叉树的所有路径
- 确定递归函数的参数和返回值:要传入根节点,记录每一条路径的path,和存放结果集的result,这里递归不需要返回值
void traversal(TreeNode* cur, vector<int>& path, vector<string>& result);
- 确定递归终止条件:因为本题在找到叶子节点之后就开始结束的处理逻辑了,所以终止条件是当前节点为叶子节点。
if(cur->left == nullptr && cur->right == nullptr){
终止处理逻辑
}
这里的终止处理逻辑是将path的结果放入到result中,同时按照题目要求,要在数字间加入规定的字符。
if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点
string sPath;
for (int i = 0; i < path.size() - 1; i++) { // 将path里记录的路径转为string格式
sPath += to_string(path[i]);
sPath += "->";
}
sPath += to_string(path[path.size() - 1]); // 记录最后一个节点(叶子节点)
result.push_back(sPath); // 收集一个路径
return;
}
- 确定单层递归逻辑
因为是前序遍历,需要先处理中间节点,中间节点就是我们要记录路径上的节点,先放进path中。
path.push_back(cur->val);
然后是递归和回溯的过程,上面说过没有判断cur是否为空,那么在这里递归的时候,如果为空就不进行下一层递归了。
if (cur->left) {
traversal(cur->left, path, result);
path.pop_back(); // 回溯
}
if (cur->right) {
traversal(cur->right, path, result);
path.pop_back(); // 回溯
}
注意,回溯和递归是一一对应的,有一个递归,就要有一个回溯。
完整的代码如下:
class Solution {
private:
void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {
path.push_back(cur->val); // 中,中为什么写在这里,因为最后一个节点也要加入到path中
// 这才到了叶子节点
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) { // 左
traversal(cur->left, path, result);
path.pop_back(); // 回溯
}
if (cur->right) { // 右
traversal(cur->right, path, result);
path.pop_back(); // 回溯
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
题目三:LeetCode 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:
int sumOfLeftLeaves(TreeNode* root) {
stack<TreeNode*> stk;
int sum = 0;
stk.push(root);
while(!stk.empty())
{
TreeNode* node = stk.top();
stk.pop();
if(node->left != nullptr && node->left->left == nullptr
&& node->left->right == nullptr)
sum += node->left->val;
if(node->left) stk.push(node->left);
if(node->right) stk.push(node->right);
}
return sum;
}
};
上面的代码中从栈中弹出一个节点,用node指向该节点,但是真正处理的是该节点的左节点,用node->left != nullptr 判断是否有左节点,如果有,就通过node->left->left == nullptr
&& node->left->right == nullptr 判断该节点是不是叶子节点。
递归法
1.确定递归函数的参数和返回值
参数:节点
返回值:数值之和
int sumOfLeftLeaves(TreeNode* root);
- 确定递归终止条件:如果传入的节点为空时,返回0
if(root == nullptr) return 0;
- 确定单层递归逻辑:当遇到左叶子节点的时候,记录数值,然后通过递归求取左子树左叶子之和,和 右子树左叶子之和,相加便是整个树的左叶子之和。
int leftValue = sumOfLeftLeaves(root->left); // 左
if (root->left && !root->left->left && !root->left->right) {
leftValue = root->left->val;
}
int rightValue = sumOfLeftLeaves(root->right); // 右
int sum = leftValue + rightValue; // 中
return sum;
完整的代码如下:
/**
* 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 == nullptr) return 0;
int leftValue = sumOfLeftLeaves(root->left); // 左
if (root->left && !root->left->left && !root->left->right) {
leftValue = root->left->val;
}
int rightValue = sumOfLeftLeaves(root->right); // 右
int sum = leftValue + rightValue; // 中
return sum;
}
};