代码随想录算法训练营第十七天| 654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

今日任务:


654.最大二叉树 
617.合并二叉树 
700.二叉搜索树中的搜索 
98.验证二叉搜索树 


654.最大二叉树 


题目链接: . - 力扣(LeetCode)

/**
 * 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 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        if (nums.length == 0) return null;
        return buildhelper(nums, 0, nums.length - 1);
    }

    private TreeNode buildhelper(int[] nums, int start, int end){
        if (start > end) {return null;}
        int max = Integer.MIN_VALUE;
        int index = -1;
        for (int i = start; i <= end; i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = buildhelper(nums, start, index - 1);
        root.right = buildhelper(nums, index + 1, end);
        return root;
    }

}

617.合并二叉树 


题目链接: . - 力扣(LeetCode)

/**
 * 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 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (root1 == null){return root2;}
        if (root2 == null) {return root1;}
        stack.add(root2);
        stack.add(root1);
        while(!stack.isEmpty()){
            TreeNode node1 = stack.pop();
            TreeNode node2 = stack.pop();
            node1.val += node2.val;

            if(node1.right != null && node2.right != null){
                stack.add(node2.right);
                stack.add(node1.right);
            } else {
                if (node1.right == null) {
                    node1.right = node2.right;
                }
            }

            if(node1.left != null && node2.left != null){
                stack.add(node2.left);
                stack.add(node1.left);
            } else {
                if (node1.left == null) {
                    node1.left = node2.left;
                }
            }
        }
        return root1;
    }

}


700.二叉搜索树中的搜索 


题目链接: . - 力扣(LeetCode)

/**
 * 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 {
    public TreeNode searchBST(TreeNode root, int val) {
        /*
        * 迭代法
        * */
        if (root == null || root.val == val) {
            return root;
        }

        if (root.val > val) {
            return searchBST(root.left, val);
        } else {
            return searchBST(root.right, val);
        }
    }
}


98.验证二叉搜索树 


题目链接: . - 力扣(LeetCode)

/**
 * 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 {
    public boolean isValidBST(TreeNode root) {
       /*
        * 使用迭代吗  统一迭代法
        * */
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (root == null) return true;
        Queue<Integer> queue = new LinkedList<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode tmpNode = stack.peek();
            if (tmpNode != null) {
                stack.pop();
                if (tmpNode.right != null) {stack.push(tmpNode.right);}
                stack.push(tmpNode);
                stack.push(null);
                if (tmpNode.left != null) {stack.push(tmpNode.left);}
            } else {
                stack.pop();
                tmpNode = stack.pop();
                queue.offer(tmpNode.val);
            }
        }
        int[] result = new int[queue.size()];
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            result[i] = queue.poll();
        }
        for (int i = 0; i < result.length - 1; i++) {
            if (result[i] >= result[i + 1]) {
                return false;
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值