110. 平衡二叉树
题目链接
思路
首先需要明确,这道题是在考察平衡二叉树的定义。任何一个节点左右子树的高度相差不超过1则为平衡二叉树。联想到最大深度的解法,我们选择递归法来做,返回值为int,入口参数为节点,结束条件是当前节点为空,则返回0.那么如何标记非平衡二叉树呢?如果已经不满足了,则让它返回-1,否则返回该节点的最大高度。最后根据递归函数返回值确定是否是平衡二叉树。
文章详解
cpp
/**
* 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 lh = getheight(node->left);
if(lh == -1) return -1;
int rh = getheight(node->right);
if(rh == -1) return -1;
int n = abs(rh - lh);
if(n <= 1) return 1 + max(rh,lh);
else return -1;
}
bool isBalanced(TreeNode* root) {
int res = getheight(root);
if(res >= 0) return true;
else return false;
}
};
257. 二叉树的所有路径
题目链接
思路
找到所有路径,我们需要回溯。在找到一条路径走到底时,跳转至它的父节点。选择递归方法来做时,函数返回值为void,入口参数需要当前节点,已经走过的节点栈(便于回溯,需要回溯时栈顶出栈即可),结果集(保存路径)。结束递归的条件是,当前节点为叶子节点。 递归内部实现:首先当前节点加入结果集,然后将其入栈,去找左子树的路径;完成后找右子树的路径,最后返回结果集。
需要注意:左子树和右子树在递归前,需要先判断是否有子节点。以及回溯和递归要写在一起。
文章详解
cpp
class Solution {
private:
void traversal(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) { // 左
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;
}
};
404. 左叶子之和
题目链接
思路
明确什么是左叶子之和:所有的左叶子节点之和。怎么判断呢?父节点有左孩子,且该左孩子的左右子节点为空。之后遍历就可以了。
文章详解
cpp
/**
* 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 == NULL) return 0;
if(root->left == NULL && root->right == NULL) return 0;
int leftvalue = sumOfLeftLeaves(root->left);
if(root->left && root->left->left == NULL && root->left->right == NULL)
{
leftvalue = root->left->val;
}
int rightvalue = sumOfLeftLeaves(root->right);
int sum = leftvalue + rightvalue;
return sum;
}
};
222. 完全二叉树的节点个数
题目链接
222. 完全二叉树的节点个数 - 力扣(LeetCode)
思路
迭代法层序遍历。如果要优化,想法是计算层数,最后一层之前的不需要遍历,每一层节点个数为2^(n-1),只需要遍历最后一层。
文章详解
cpp
/**
* 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 countNodes(TreeNode* root) {
queue <TreeNode*> que;
int res = 0;
if(root == NULL) return res;
que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size; i++)
{
TreeNode* cur = que.front();
que.pop();
res++;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return res;
}
};