代码随想录算法训练营第十五天| 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和、 222.完全二叉树的节点个数

Leetcode110.平衡二叉树

题目链接:110. 平衡二叉树

C++:(后序遍历)

class Solution {
public:
    int getheight(TreeNode *node){
        if(node == nullptr) return 0;
        //左
        int leftheight = getheight(node->left);
        if(leftheight == -1) return -1;
        //右
        int rightheight = getheight(node->right);
        if(rightheight == -1) return -1;
        //中
        int result;
        if(abs(leftheight - rightheight) > 1)
            result = -1;
        else
        {
            result = 1 + max(leftheight, rightheight);
        }
        return result;
    }

    bool isBalanced(TreeNode* root) {
        return getheight(root) == -1 ? false : true;
    }
};

Python:

class Solution:
    def getheight(self, node):
        if node == None:
            return 0
        leftheight = self.getheight(node.left)
        if leftheight == -1:
            return -1
        rightheight = self.getheight(node.right)
        if rightheight == -1:
            return -1
        if abs(leftheight - rightheight) > 1:
            result = -1
        else:
            result = 1 + max(leftheight, rightheight)
        return result

    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        return False if self.getheight(root) == -1 else True

Leetcode257. 二叉树的所有路径

题目链接:257. 二叉树的所有路径

C++:(回溯算法初体验)

注意:传入的形参为引用格式,递归+回溯成对出现

class Solution {
public:
    void findway(TreeNode* node, vector<int> &path, vector<string> &result){
        //中
        path.push_back(node->val);
        if(node->left == nullptr && node->right == nullptr)
        {
            string sPath;
            for(int i=0; i < path.size() - 1; i++)
            {
                sPath += to_string(path[i]);
                sPath += "->";
            }
            sPath += to_string(path[path.size() - 1]);
            result.push_back(sPath);
            return;
        }
        //左
        if(node->left != nullptr)
        {
            findway(node->left, path, result);
            path.pop_back();  //回溯
        }
        //右
        if(node->right != nullptr)
        {
            findway(node->right, path, result);
            path.pop_back();  //回溯
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if (root == NULL) return result;
        findway(root, path, result);
        return result;
    }
};

Python:

map()函数:会根据提供的函数对指定序列做映射,语法:map(function, iterable)

     (1)function:函数

       (2) iterable:一个或多个序列

class Solution:
    def findway(self, node, path, result):
        # zhong
        path.append(node.val)
        if node.left == None and node.right == None:
            sPath = '->'.join(map(str, path))
            result.append(sPath)
            return
        if node.left:
            self.findway(node.left, path, result)
            path.pop()
        if node.right:
            self.findway(node.right, path, result)
            path.pop()


    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        path = []
        result = []
        if root == None:
            return result
        self.findway(root, path, result)
        return result

Leetcode404.左叶子之和

题目链接:404. 左叶子之和

C++:

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(root == nullptr) return 0;
        if(root->left == nullptr && root->right == nullptr) return 0;
        //zuo
        int Leftsum = sumOfLeftLeaves(root->left);
        if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr)
            Leftsum = root->left->val;
        //you
        int Rightsum = sumOfLeftLeaves(root->right);
        //zhong
        int sum = Leftsum + Rightsum;
        return sum;
    }
};

Leetcode 222.完全二叉树的节点个数

题目链接:222. 完全二叉树的节点个数

完全二叉树的结点数量可以通过满二叉树结点数量公式计算:2**depth - 1

C++:

class Solution {
public:
    int countNodes(TreeNode* root) {
        //后序遍历
        if(root == nullptr) return 0;
        auto left = root->left;
        int leftdepth = 0;
        auto right = root->right;
        int rightdepth = 0;
        while(left)
        {
            left = left->left;
            leftdepth++;
        }
        while(right)
        {
            right = right->right;
            rightdepth++;
        }
        if(leftdepth == rightdepth)
            return (2 << leftdepth) - 1;
        //zuo
        int leftnum = countNodes(root->left);
        //you
        int rightnum = countNodes(root->right);
        //zhong
        int num = leftnum + rightnum + 1;
        return num;
    }
};
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值