力扣_Hot_二叉树

简单

1.二叉树的中序遍历
给定一个二叉树的根节点 root ,返回它的 中序 遍历。
在这里插入图片描述

class Solution {
   public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        // 递归
        /*
        inorder(root, result);
        return result;
        */
        //迭代,基于栈的中序遍历
        Stack<TreeNode> stack = new Stack<>();  //此处栈存放 TreeNode
        TreeNode cur = root;
        while (cur != null || !stack.empty()){
            if (cur != null){ // 当前节点不空时,入栈;指针左下移;
                stack.push(cur);
                cur = cur.left;
            }else{ // 当前节点为空时,指针指向栈顶元素,并将其值加入列表;指针右下移
                cur = stack.pop();
                result.add(cur.val);
                cur = cur.right;
            }
        }
        return result;

    }
    /* 递归涉及的函数
    public void inorder(TreeNode root,List<Integer> result){
        if (root == null){
            return;
        }
        inorder(root.left, result);
        result.add(root.val);
        inorder(root.right, result);
    }
    */
}

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

class Solution {
     public boolean isSymmetric(TreeNode root) {
        // 比较 ①左子树的右孩子 与 右子树的左孩子;② 左子树的左孩子 与 右子树的有孩子
       
        if (root==null)
            return true;
        else 
            return dfs(root.left, root.right);
    }
    public boolean dfs(TreeNode left, TreeNode right){
        if (left==null && right==null)
            return true;
        if (left==null || right==null)
            return false;
        if (left.val != right.val)
            return false;
        return dfs(left.left, right.right) && dfs(left.right, right.left);
    }
}

3.二叉树的最大深度
给定一个二叉树,找出其最大深度。
(二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。)
说明: 叶子节点是指没有子节点的节点。

class Solution {
    public int maxDepth(TreeNode root) {
        //int depth = 0;
        if (root==null)
            return  0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left,right)+1;
    }
}

4.翻转二叉树
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
在这里插入图片描述

class Solution {
     public TreeNode invertTree(TreeNode root) {
        /*
          递归解法:首先将二叉树左右子树分别翻转,之后再将左右子树对调
         */
        if (root==null || (root.right==null && root.left==null))
            return root;
        invertTree(root.left); // 递归调用:翻转左右子树
        invertTree(root.right);
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        return root;
    }
}

5.二叉树的直径
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
在这里插入图片描述

class Solution {
    int max = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        /*
          * 求解二叉树的直径:任意两个节点路径长度中的最大值,也就是两节点之间边的数目
          * 将问题转换为:求二叉树的每个节点的左右子树的高度和的最大值
          * 以根节点为起点,求左子树和右子树的深度depth_1,depth_2,直径=depth_1+depth2-1
         */
        depth(root);
        return max;
    }
    public int depth(TreeNode root){
        if (root==null) return 0;
        int depth_left = depth(root.left); // 左儿子为根的子树深度
        int depth_right = depth(root.right); // 右儿子为根的子树深度
        max = Math.max(depth_left + depth_right, max); // root的最大直径
        return Math.max(depth_left, depth_right) + 1; // 返回root节点深度
    }
}

6.合并二叉树
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
在这里插入图片描述

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1!=null&&root2!=null){
            root1.val = root1.val + root2.val;
        }else if (root1==null){
            return root2;
        }else return root1;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值