首页 > 其他 > 详细

[树] leetcode 814 Binary Tree Pruning

时间:2019-08-02 12:48:42      阅读:86      评论:0      收藏:0      [点我收藏+]

problem:https://leetcode.com/problems/binary-tree-pruning/

        一道非常简单的tree题目,根据题意模拟剔除不包含0的子树即可。

class Solution {
public:
    bool dfs(TreeNode* p)
    {
        if (!p) return false;
        bool bLeft = dfs(p->left);
        bool bRight = dfs(p->right);
        
        if (!bLeft)
        {
            p->left = nullptr;
        }
        if (!bRight)
        {
            p->right = nullptr;
        }
        if (bLeft || bRight || p->val == 1)
        {
            return true;
        }

        return false;    

    }
    TreeNode* pruneTree(TreeNode* root) {
        dfs(root);
        return root;
    }
};

 

[树] leetcode 814 Binary Tree Pruning

原文:https://www.cnblogs.com/fish1996/p/11287819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!