算法打卡,用于自律

这篇博客涵盖了四个关于二叉树操作的解题方法:计算二叉树的最大深度、找到二叉树中两个节点的最近公共祖先、将二叉搜索树转换为递增BST以及在一个二叉搜索树中查找目标和的节点。这些解法展示了对二叉树结构的深入理解和高效遍历技巧。
摘要由CSDN通过智能技术生成

题目一

 解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<List<Integer>> list = new ArrayList<List<Integer>>();
    public int maxDepth(TreeNode root) {
        method(0,root);
        return list.size();
    }
    public void method(int depth,TreeNode root){
        if(root==null) return;
        if(list.size()==depth){
            list.add(new ArrayList<Integer>());
        }
        method(depth+1,root.left);
        method(depth+1,root.right);
    }
}

题目二

 解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ans = root;
        while(true){
            if(p.val<ans.val&&q.val<ans.val){
                ans = ans.left;
            }else if(p.val>ans.val&&q.val>ans.val){
                ans = ans.right;
            }else{
                break;
            }
        }
        return ans;
    }
}

题目三

 解法

/**
 * 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 {
    List<Integer> list = new ArrayList<Integer>();
    public TreeNode increasingBST(TreeNode root) {
        method(root);
        TreeNode tmp = new TreeNode(-1);
        TreeNode ans = tmp;
        for(int i:list){
            tmp.right = new TreeNode(i);
            tmp = tmp.right;
        }
        return ans.right;
    }
    public void method(TreeNode root){
        if(root==null) return;
        method(root.left);
        list.add(root.val);
        method(root.right);
    }
}

题目四

 解法

/**
 * 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 {
    Set<Integer> set = new HashSet<Integer>();
    public boolean findTarget(TreeNode root, int k) {
        if (root == null) return false;
        if (set.contains(k - root.val)) return true;
        set.add(root.val);
        return findTarget(root.left, k) || findTarget(root.right, k);
    }
}

题目五

 解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return method(nums,0,nums.length-1);
    }
    public TreeNode method(int[] nums,int low,int high){
        if(low>high) return null;
        int mid = low+(high-low)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = method(nums,low,mid-1);
        root.right = method(nums,mid+1,high);
        return root;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值