Leetcode打卡day14-二叉树2

226.翻转二叉树

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

思路:递归前序遍历,然后在里面翻转每一个节点的左右孩子。

c++

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        // 终止条件
        if (root== nullptr) return root;
        // 单层逻辑
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

python

class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root:
            return None
        root.left, root.right = root.right, root.left
        self.invertTree(root.left)
        self.invertTree(root.right)
        return root

 101. 对称二叉树 

给你一个二叉树的根节点 root , 检查它是否轴对称。

思路:遍历两棵树而且要比较内侧和外侧节点,一个树遍历左右中,一个树遍历是右左中

c++

class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right) {
        if (left == nullptr && right == nullptr)
            return true;
        else if (left != nullptr && right == nullptr)
            return false;
        else if (left == nullptr && right != nullptr)
            return false;
        else if (left->val != right->val)
            return false;
        //比较
        else
            return compare(left->left, right->right) &&
                   compare(left->right, right->left);
    }
    bool isSymmetric(TreeNode* root) {
        if (root == nullptr)
            return true;
        return compare(root->left, root->right);
    }
};

python

class Solution:
    def isSameTree(self, left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
        if left is None or right is None:
            return left is right
        return left.val == right.val and self.isSameTree(left.left,right.right) and self.isSameTree(left.right, right.left)

    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        return self.isSameTree(root.left, root.right)

104.二叉树的最大深度

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

思路:根节点的高度就是二叉树的最大深度

递归参数和返回值:根节点,返回树的深度

终止条件:如果节点为空返回0,表示高度为0

单层逻辑:先求左子树深度,再求右子树深度,最后取左右深度最大的数字再+1(要算上当前的中间节点),即为目前节点为根节点的树的深度。

c++

class Solution {
public:
    int getdepth(TreeNode* node){
        if(node==nullptr) return 0;
        int leftdepth = getdepth(node->left);
        int rightdepth = getdepth(node->right);
        int depth = 1+max(leftdepth,rightdepth);
        return depth;
    }
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};

python

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

111.二叉树的最小深度 

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

思路:递归

单层逻辑:如果左子树为空右子树不为空,最小深度为1+右子树深度

如果右子树为空,左子树不为空,最小深度为1+左子树深度

如果左右子树都不为空,返回左右子树深度最小值+1

c++

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr)
            return 0;
        if (root->left == nullptr && root->right != nullptr) {
            return 1 + minDepth(root->right);
        }
        if (root->left != nullptr && root->right == nullptr) {
            return 1 + minDepth(root->left);
        }
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};

python

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        if root.left is None and root.right is not None:
            return 1 + self.minDepth(root.right)
        if root.left is not None and root.right is None:
            return 1 + self.minDepth(root.left)
        return 1 + min(self.minDepth(root.left), self.minDepth(root.right))

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值