110 Height-Balanced
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
树的深度:root到最远leaf node
这里关于高度和深度明确一下:leetcode的node深度,root是1
我写的递归
int get_depth(TreeNode* node){
if(node==nullptr) return 0;
int left_d=0; int right_d=0;
if(node->left!=nullptr) left_d=get_depth(node->left);
if(node->right!=nullptr) right_d=get_depth(node->right);
return std::max(left_d,right_d)+1;
}
bool isBalanced(TreeNode * node){
if(node==nullptr) return true;
if (abs(get_depth(node->left)-get_depth(node->right))>1) return false;
return isBalanced(node->left)&&isBalanced(node->right);
}
我感觉这题迭代会非常麻烦,所以选择不做迭代。代码随想录也说:
虽然理论上所有的递归都可以用迭代来实现,但是有的场景难度可能比较大。
例如:都知道回溯法其实就是递归,但是很少人用迭代的方式去实现回溯算法!
因为对于回溯算法已经是非常复杂的递归了,如果再用迭代的话,就是自己给自己找麻烦,效率也并不一定高。
“通过本题可以了解求二叉树深度 和 二叉树高度的差异,求深度适合用前序遍历,而求高度适合用后序遍历。”我一直没太理解到这点,因为我感觉深度和高度是能互相转换的,所以我没把他们分那么清楚
257.所有路径 ! 在这题里学到了递归与回溯的关系,很重要
我最开始写层序遍历的递归,传参数depth时发现不能传ref,而且不能先depth++ 再call,而必须func(depth+1,...),其实这就是隐式的回溯,而先depth++,func(depth),再depth--,这样是显式的回溯。下面的例子也是这个意思
再看我自己写的递归:其实我觉得我根本没做回溯,详见下面和gpt的对话
void getP(TreeNode * node, vector<string> &vstr, string path){
if(node==nullptr) return;
//append the new node
if(path=="")path.append(to_string(node->val));
else path.append("->"+to_string(node->val));
if(node->left==nullptr && node->right==nullptr){//leaf node
//add to vec
vstr.push_back(path);
//这里居然不要写path.clear();也是对的
}else{
getP(node->left,vstr,path);
getP(node->right,vstr,path);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> vstr;
string path="";
getP(root, vstr, path);
return vstr;
}
代码随想录递归这么写的:
隐式回溯特点:如果把 path + "->"
作为函数参数就是可以的,因为并没有改变path的数值,执行完递归函数之后,path依然是之前的数值(相当于回溯了)
class Solution {
private:
void traversal(TreeNode* cur, string path, vector<string>& result) {
path += to_string(cur->val); // 中
if (cur->left == NULL && cur->right == NULL) {
result.push_back(path);
return;
}
if (cur->left) traversal(cur->left, path + "->", result);
if (cur->right) traversal(cur->right, path + "->", result);
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
string path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
string的pop_back: erase last char, reduce len by 1
其实我的递归和他的区别不大,自己对这题说的回溯还是略有疑惑,没完全懂
所有路径之和本题要做迭代法的话,是做dfs的统一迭代,或者随想录用的是dfs前序的自己的迭代,用层序遍历不合适。
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;
}
404 左子叶之和
我写的递归
void recursion(TreeNode* node, int &sum){
if(node==nullptr) return;
TreeNode * leftc=node->left;
if(leftc!=nullptr && leftc->left==nullptr && leftc->right==nullptr){
//cout<<"i am "<<node->val<<", yes i have leaf left: "<<leftc->val<<endl;
sum+=leftc->val;
}
if(node->left!=nullptr) recursion(node->left, sum);
if(node->right!=nullptr) recursion(node->right, sum);
}
int sumOfLeftLeaves(TreeNode* root) {
int sum=0;
recursion(root,sum);
return sum;
}
我一直不太会写那种只有一个函数的递归,附上随想录的:不过他这就是后序了
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 && !root->left->right) { // 左子树就是一个左叶子的情况
leftValue = root->left->val;
}
int rightValue = sumOfLeftLeaves(root->right);
int sum = leftValue + rightValue;
return sum;
}
迭代的话,套dfs迭代的模板,前序非统一的,和统一的前中后序,都行。