代码随想录day15|110,257,404,222

文章链接:

代码随想录代码随想录PDF,代码随想录网站,代码随想录百度网盘,代码随想录知识星球,代码随想录八股文PDF,代码随想录刷题路线,代码随想录知识星球八股文icon-default.png?t=N7T8https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html

110

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root == NULL)
            return true;
        if(abs(maxDepth(root->left)-maxDepth(root->right))>1)
            return false;
        if(isBalanced(root->left)&&isBalanced(root->right))
            return true;
        return false;
    }

    int maxDepth(TreeNode* root){
        if(root == NULL)
            return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};

 257

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        
    }
    LinkedList<String>path = new LinkedList<>();
    LinkedList<String>res = new LinkedList<>();

    void traverse(TreeNode root){
        if(root == null){
            return;
        }

        if(root.left == null && root.right == null){
            path.addLast(root.val+"");

            res.addLast(String.join("->",path));
            path.removeLast();
            return;
        }

        path.addLast(root.val+"");
        traverse(root.left);
        traverse(root.right);
        path.removeLast();
    }
}

404

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        traverse(root);
        return sum;
    }

  
    int sum = 0;
   
    void traverse(TreeNode* root) {
        if (root == nullptr) {
            return;
        }

        if (root->left != nullptr && root->left->left == nullptr &&
            root->left->right == nullptr) {
            
            sum += root->left->val;
        }

        
        traverse(root->left);
        traverse(root->right);
    }
};

222

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int cnt = 0;
    int countNodes(TreeNode* root) {
         TreeNode* l = root;
         TreeNode* r= root;

         int hl = 0,hr = 0;
         while(l != nullptr){
            l = l->left;
            hl++;
         }
         while(r !=nullptr){
            r = r->right;
            hr++;
         }
         if(hl == hr){
            return (int)pow(2,hl)-1;
         }
         return 1+ countNodes(root->left)+countNodes(root->right);
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值