文章目录
Day 17 二叉树Part 4
110. 平衡二叉树
O(n^2)的解法:从根节点开始,获取当前节点的高度,再比较下面的子节点。显然这种前序遍历会导致高度的重复计算。
class Solution {
public:
int getHeight(TreeNode* root) {
if (root == NULL) return 0;
return 1 + max(getHeight(root->left), getHeight(root->right));
}
bool isBalanced(TreeNode* root) {
if (root == NULL) return true;
if (abs(getHeight(root->left) - getHeight(root->right)) > 1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
};
O(n)解法:改进思路为在求高度的时候就顺便把子节点是否为平衡二叉树的判断做完,相当于后序遍历:
class Solution {
public:
int getHeight(TreeNode* root) {
if (root == NULL) return 0;
int leftHeight = getHeight(root->left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(root->right);
if (rightHeight == -1) return -1;
//if (leftHeight == -1 || rightHeight == -1) return -1;
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
257. 二叉树的所有路径
递归思路:输出的字符串向量在递归时作为参数传递;另外传递一个字符串变量,在当前节点不为空时累加,并在不为叶子节点时加箭头,并继续向下遍历。当判断为叶子节点时,当前路径加入字符串向量中。
class Solution {
public:
void addPath(TreeNode* node, string path, vector<string>& paths) {
if (node == NULL) return;
path += to_string(node->val); //注意这里需要将节点值转化为字符串
if (!node->left && !node->right) paths.push_back(path);
else {
path += "->";
addPath(node->left, path, paths);
addPath(node->right, path, paths);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> paths;
addPath(root, "", paths);
return paths;
}
};
404. 左叶子之和
思路:在递归函数中加入一个判断当前子节点是否为左子节点的变量。
class Solution {
public:
int add(TreeNode* node, bool isLeftTree) {
if (node == NULL) return 0;
if (!node->left && !node->right && isLeftTree) return node->val;
return add(node->left, true) + add(node->right, false);
}
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;
return add(root->left, true) + add(root->right, false);
}
};
也可以直接在遍历父节点时取其左子节点判断是否为叶子,只有在是的情况下才加和。这种方法就不需要额外构造一个递归函数了。
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;
int leftValue = 0;
if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) {
leftValue = root->left->val;
}
return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
Day 18 二叉树Part 5
513. 找树左下角的值
关键思路:使用两个全局变量来储存当前遍历的最大深度和当前最左的结果。并且判断当前深度要大于最大深度才更换结果(操作1)+先遍历左子树(操作2)这两个操作保证了当处于同一层时一定储存的是最左的节点。
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth) {
if (root->left == NULL && root->right == NULL) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
return;
}
if (root->left) traversal(root->left, depth + 1);
if (root->right) traversal(root->right, depth + 1);
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return result;
}
};
112. 路径总和 & 113. 路径总和 II
112:思路比较简单。
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == NULL) return false;
targetSum -= root->val;
if (targetSum == 0 && !root->left && !root->right) return true;
return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
}
};
113:借鉴层序遍历的迭代写法。
class Solution {
public:
void findPath(TreeNode* node, int targetSum, vector<int> path, vector<vector<int>>& paths) {
if (node == NULL) return;
targetSum -= node->val;
path.push_back(node->val);
if (targetSum == 0 && !node->left && !node->right) paths.push_back(path);
else {
findPath(node->left, targetSum, path, paths);
findPath(node->right, targetSum, path, paths);
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> paths;
findPath(root, targetSum, vector<int>(), paths);
return paths;
}
};