今日任务:
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;
}
}