563.
class Solution {
public:
int ans =0;
int findTilt(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root){
if(root==nullptr) return 0;
int l, r;
if(root->left) {
l = dfs(root->left) + root->left->val;
}
if(root->right) r = dfs(root->right);
ans += abs(l-r);
return abs(l-r);
}
};
//以上错的,纠缠不清
class Solution {
public:
int findTilt(TreeNode* root) {
if(root==nullptr) return 0;
return abs(dfs(root->left) - dfs(root->right)) + findTilt(root->left) + findTilt(root->right);
}
int dfs(TreeNode* root){
if(root==nullptr) return 0;
return root->val + dfs(root->left) + dfs(root->right);
}
};
//用了太多次递归,效率不行,这个好些、
class Solution {
public:
int ans = 0;
int findTilt(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root){ //左右都加上了根节点,相减的时候就抵消了
if(root == nullptr) return 0;
int l = dfs(root->left);
int r = dfs(root->right);
ans += abs(l-r);
return l+r + root->val;
}
};
814.
- 最暴力的全部遍历列举了
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root == nullptr) return root;
if(dfs(root) == false) return nullptr;
if(dfs(root->left)== false) root->left = nullptr;
if(dfs(root->right)== false) root->right = nullptr;
pruneTree(root->left);
pruneTree(root->right);
return root;
}
bool dfs(TreeNode* root){ //只要出现了一个1, true,这棵树不能被删
if(root==nullptr) return false;
return root->val == 1 || dfs(root->left) || dfs(root->right);
}
};
牛,抓住每次prunTree, return出的,
从下往上一点点删。底部肉眼所见共同性全部已被删。
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root == nullptr) return nullptr;
TreeNode* l = pruneTree(root->left);
TreeNode* r = pruneTree(root->right);
if(!l && !r && root->val==0) return nullptr;
return root;
}
};
//以上有误
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root == nullptr) return nullptr;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(!root->left && !root->right && root->val==0) return nullptr;
return root;
}
};
589.
class Solution {
public:
vector<int> ans;
vector<int> preorder(Node* root) {
dfs(root);
return ans;
}
void dfs(Node* root){
if(root==NULL) return;
ans.push_back(root->val);
for(auto a: root->children){
dfs(a);
}
}
};
//为什么用queue不可以,只能stack??
class Solution {
public:
vector<int> preorder(Node* root) {
queue<Node*> q;
q.push(root);
vector<int> ans;
while(!q.empty()){
Node* front = q.front();
ans.push_back(front->val);
q.pop();
for(int i=0; i<front->children.size(); i++){
q.push(front->children[i]);
}
}
return ans;
}
};
429.
- 觉得先后顺序烦?stack?queue告诉你有先进先出的!!
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> ans;
if(root == NULL) return ans;
queue<Node*> stk;
stk.push(root);
while(!stk.empty()){
int n = stk.size();
vector<int> tmp;
for(int i=0; i<n; i++){
Node* top = stk.front();
tmp.push_back(top->val);
stk.pop();
for(auto a: top->children){
stk.push(a);
}
}
ans.push_back(tmp);
}
return ans;
}
};
590.
- 后序遍历就reverse先序!!!
//为何此法超出时间限制??不可??
class Solution {
public:
vector<int> postorder(Node* root) {//前序遍历加个reverse不就可以吗
stack<Node*> stk;
vector<int> ans;
stk.push(root);
while(!stk.empty()){
Node* top = stk.top();
stk.pop();
ans.push_back(top->val);
for(int i =0; i<top->children.size(); i++){
stk.push(root->children[i]);
}
}
return ans;
}
};
//还是用了递归?
class Solution {
public:
vector<int> ans;
vector<int> postorder(Node* root) {
dfs(root);
reverse(ans.begin(), ans.end());
return ans;
}
void dfs(Node* root){
if(root==NULL) return;
ans.push_back(root->val);
for(int i = root->children.size()-1; i>=0; i--){
dfs(root->children[i]);
}
}
};