110.平衡二叉树
class Solution {
public:
int getHeight(TreeNode* node){
if(node ==NULL)return 0;
int result;
int leftHeight = getHeight(node->left);
//如果该左子树不是平衡二叉树,就返回-1
if(leftHeight == -1)return -1;
int rightHeight = getHeight(node->right);
//同理
if(rightHeight == -1)return -1;
//如果该节点的左右子树高度差大于1,那么就返回-1
if(abs(leftHeight - rightHeight) > 1)return -1;
else{
result = 1+max(leftHeight, rightHeight);
}
return result;
}
bool isBalanced(TreeNode* root) {
return getHeight(root)==-1 ? false:true;
}
};
257. 二叉树的所有路径
递归法:
1. path.push_back(node->val); 这个语句要写在前面,因为要将叶子结点存到路径中,然后判断当前节点的左右孩子是否存在;
2.vector<int>& path,这里使用引用的方式而不是vector<int> path,因为在迭代回溯过程中,要对原始path一直进行修改。而vector<int> path,在进行右子树遍历时,会创建新的path对象,对这个对象的修改不会影响传递进来的原始 path
对象,因此会发生错误。
class Solution {
private:
//递归法:由于输出的是从根节点到叶子结点的路径,因此是从上往下进行遍历,所有选择前序
void traversal(TreeNode* node, vector<int>& path, vector<string>& result){
path.push_back(node->val); //中,写在终止条件之前是因为叶子结点也要加入path
//终止条件:当前节点为叶子节点
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);
return;
}
//单层循环逻辑
if(node->left){
traversal(node->left, path, result);
path.pop_back(); //回溯
}
if(node->right){
traversal(node->right, path, result);
path.pop_back();
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> result;
if(root == NULL) return result;
traversal(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;
pathSt.push(to_string(root->val));
treeSt.push(root);
while(!pathSt.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;
}
};
404.左叶子之和
递归法:
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root == NULL)return 0;
if(root->left == NULL && root->right == NULL)return 0;
//左子树的左叶子节点值
int leftLeaves = sumOfLeftLeaves(root->left);
//判断左叶子节点
if(root->left != NULL && root->left->left == NULL && root->left->right == NULL){
leftLeaves = root->left->val;
}
//右子树的左叶子节点值
int rightLeaves = sumOfLeftLeaves(root->right);
//中
int sum = leftLeaves + rightLeaves;
return sum;
}
};
迭代法:
class Solution {
public:
//迭代法
int sumOfLeftLeaves(TreeNode* root) {
if(root == NULL)return 0;
stack<TreeNode*> st;
st.push(root);
int result;
while(!st.empty()){
TreeNode* node;
node = st.top();
st.pop();
if(node->left && node->left->left == NULL && node->left->right == NULL){
result += node->left->val;
}
if(node->right){
st.push(node->right);
}
if(node->left){
st.push(node->left);
}
}
return result;
}
};