Java-数据结构-二叉树<二>

承接上文:Java-数据结构-二叉树<一>

一. 二叉树的简单介绍

        见Java-数据结构-二叉树<一>

二. 二叉树的典型代码实现

        见Java-数据结构-二叉树<一>

三. 二叉树的遍历

        见Java-数据结构-二叉树<一>

四. leetcode实战

1~2 见 Java-数据结构-二叉树<一>

3. leetcode100 相同的树

        给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
输入:p = [1,2,3], q = [1,2,3]
输出:true 

class Solution {
    boolean flag = true;
    public boolean isSameTree(TreeNode p, TreeNode q) {
        dfs(p,q);
        return flag;
    }
    public void dfs(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null){
            return;
        }
        if(root1 == null && root2 != null){
            flag = false;
            return;
        }
        if(root1 != null && root2 == null){
            flag = false;
            return;
        }
        if(root1.val != root2.val){
            flag = false;
            return;
        }
        dfs(root1.left,root2.left);
        dfs(root1.right,root2.right);
    }
}

精简版 

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        } else if (p.val != q.val) {
            return false;
        } else {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }
}

4. leetcode101 对称二叉树

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

输入:root = [1,2,2,3,4,4,3]
输出:true

class Solution {
    boolean flag = true;
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        dfs(root.left,root.right);
        return flag;
    }
    public void dfs(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null){
            return;
        }
        if(root1 != null && root2 == null){
            flag = false;
            return;
        }
        if(root1 == null && root2 != null){
            flag = false;
            return;
        }
        if(root1.val != root2.val){
            flag = false;
            return;
        }
        dfs(root1.left,root2.right);
        dfs(root2.left,root1.right);
    }
}

本题小结:(1)这题和 第三题本质一样,本质都在遍历并比较

                  (2)不一样的是遍历的方向,要注意左右之分。

5. leetcode110 平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

输入:root = [3,9,20,null,null,15,7]
输出:true

自下向上

public class BalancedBinaryTree {
    boolean res = true;

    public boolean isBalanced(TreeNode root) {

        helper(root);
        return res;

    }

    private int helper(TreeNode root) {
        if (root == null) return 0;
        int left = helper(root.left) + 1;
        int right = helper(root.right) + 1;
        if (Math.abs(right - left) > 1) res = false;
        return Math.max(left, right);
    }
}

自定向下 

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

    private int depth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(depth(root.left), depth(root.right)) + 1;
    }
}

6. leetcode226 翻转二叉树 

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

输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]

递归 先递归后交换

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }
}

递归 先交换后递归

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

 层序遍历


class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        Deque<TreeNode> list = new ArrayDeque<>();
        list.add(root);
        while(!list.isEmpty()){
            TreeNode node = list.pop();
            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;
            if(node.left != null){
                list.add(node.left);
            }
            if(node.right != null){
                list.add(node.right);
            }  
        }
        return root;
    }
}

 

7. leetcode543 二叉树直径

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

 

返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。

class Solution {
    int res = 0; 
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return res;
    }
    // 函数dfs的作用是:找到以root为根节点的二叉树的最大深度
    public int dfs(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftDepth = dfs(root.left);
        int rigthDepth = dfs(root.right);
        res = Math.max(res,leftDepth + rigthDepth);
        return Math.max(leftDepth,rigthDepth) + 1;
    }
}

 

8. leetcode617 合并二叉树

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

输入:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
输出:[3,4,5,5,4,null,7]

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;
    }
}

 

 9. leetcode108 将有序数组转换为二叉搜索树

        给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums,0,nums.length-1);
    }
    public TreeNode dfs(int[] nums, int left, int right){
        if(left > right){
            return null;
        }
        int mid = (left+right)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = dfs(nums, left ,mid-1);
        root.right = dfs(nums, mid+1, right);
        return root;
    }
}

 10. leetcode700 二叉搜索树中的搜索

        给定二叉搜索树(BST)的根节点 root 和一个整数值 val。你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。

输入:root = [4,2,7,1,3], val = 2
输出:[2,1,3]

 

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null) return null;
        if(root.val == val) return root;
        if(root.val > val) return searchBST(root.left,val);
        return searchBST(root.right,val);
    }
}

 11. leetcode938 二叉搜索树的范围和

给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

输入:root = [10,5,15,3,7,null,18], low = 7, high = 15
输出:32

 

class Solution {
    public int rangeSumBST(TreeNode root, int low, int high) {
        if(root == null) return 0;
        if(root.val < low) return rangeSumBST(root.right, low, high);
        if(root.val > high) return rangeSumBST(root.left, low, high);
        return root.val + rangeSumBST(root.right, low, high)+ rangeSumBST(root.left, low, high);
    }
}

 

 

参考自

【1】leetcode powcai 自顶向下和自底向上

【2】leetcode Krahets 平衡二叉树(从底至顶,从顶至底)

【3】leetcode sugar  Java 深度优先遍历 DFS

【4】leetcode Sweetiee 🍬 简单递归,🤷‍♀️ 必须秒懂!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值