2021-05-09树563/814/589/429/590/

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]);
        }
        
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值