654.最大二叉树
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return constructMaximumBinaryTree1(nums,0,nums.length);
}
public TreeNode constructMaximumBinaryTree1(int[] nums,int leftIndex,int rightIndex){
if(rightIndex - leftIndex < 1){ //区间没有元素
return null;
}
//区间只有一个元素 直接为叶子节点或者根节点
if(rightIndex - leftIndex == 1){
return new TreeNode(nums[leftIndex]);
}
//找到nums数组中最大值以及下标 根节点
int index = leftIndex;
int maxValue = nums[index];
for(int i = leftIndex+1;i < rightIndex ;i++){
if(nums[i] > maxValue){
maxValue = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(maxValue);
//根据maxIndex划分左右子树
root.left = constructMaximumBinaryTree1(nums,leftIndex,index);
root.right = constructMaximumBinaryTree1(nums,index+1,rightIndex);
return root;
}
}
617.合并二叉树
递归
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null) return root2;
if(root2 == null) return root1;
root1.val = root1.val + root2.val;
root1.left = mergeTrees(root1.left,root2.left);
root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}
队列迭代 只操作root1树 所以无需考虑root2树中节点为空root1不为空的情况 如果为空 直接使用root1值
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null) return root2;
if(root2 == null) return root1;
Queue<TreeNode> que = new LinkedList<>();
que.add(root1);
que.add(root2);
while(!que.isEmpty()){
TreeNode node1 = que.poll();
TreeNode node2 = que.poll();
node1.val = node1.val + node2.val;
//如果两树左节点都不为空
if(node1.left != null && node2.left != null){
que.add(node1.left);
que.add(node2.left);
}
//如果两树右节点都不为空
if(node1.right != null && node2.right != null){
que.add(node1.right);
que.add(node2.right);
}
if(node1.left == null && node2.left != null){
node1.left = node2.left;
}
if(node1.right == null && node2.right != null){
node1.right = node2.right;
}
}
return root1;
}
}
700.二叉搜索树中的搜索
二叉搜索树是一个有序树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树
这就决定了,二叉搜索树,递归遍历和迭代遍历和普通二叉树都不一样。在搜索值时无需想普通二叉树前中后序全部遍历,以及无需回溯
递归代码 利用二叉树搜索特点
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val){
return root;
}
TreeNode resNode = null;
if(root.val > val){
resNode = searchBST(root.left,val);
}
if(root.val < val){
resNode = searchBST(root.right,val);
}
return resNode;
}
}
递归代码:普通二叉树
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val){
return root;
}
TreeNode left = searchBST(root.left,val);
if(left != null){
return left;
}
return searchBST(root.right,val);
}
}
迭代代码
普通迭代:
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while(root != null){
if(val < root.val){
root = root.left;
}else if(root.val < val){
root = root.right;
}else{
return root;
}
}
return null;
}
}
栈迭代
class Solution {
// 迭代,普通二叉树
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
if (pop.val == val) {
return pop;
}
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
return null;
}
}
队列迭代+搜索二叉树特性
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null){
return root;
}
Queue<TreeNode> que = new LinkedList<>();
TreeNode res = null;
que.add(root);
while(!que.isEmpty()){
int size = que.size();
for(int i = 0;i < size; i++){
TreeNode node = que.poll();
if(node.val > val && node.left != null){
que.add(node.left);
}else if(node.val < val && node.right != null){
que.add(node.right);
}else if(node.val == val){
res = node;
}
}
}
return res;
}
}
98.验证二叉搜索树
思路一:将二叉树中序遍历完保存一个数组 如果这个数组是从小到大 那么这就是一个搜索二叉树,缺点:增加了空间消耗多创建一个数组来存储节点元素
class Solution {
public boolean isValidBST(TreeNode root) {
List<Integer> arry = new ArrayList<>();
inorder(root,arry);
for(int i = 1; i < arry.size();i++){
//遍历前一位与后一位进行比较 防止数组越界
if(arry.get(i) <= arry.get(i-1)){
return false;
}
}
return true;
}
private void inorder(TreeNode root,List<Integer> arry){
if(root == null){
return;
}
inorder(root.left,arry);
arry.add(root.val);
inorder(root.right,arry);
}
}
思路二:在遍历中就进行大小比较,初始一个节点前面的值 相当于中序遍历 前一个节点与后一个节点连续比较,缺点:如果数据精度跟小就要变化
class Solution {
//后台测试数据中有int最小值 只有比int最小值更小才能更新pre的值
private long pre = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null){
return true;
}
//左
boolean left = isValidBST(root.left);
if(!left){
return false;
}
//中序遍历,验证遍历的元素是不是从小到大
if(pre < root.val){
pre = root.val;
}else{
return false;
}
boolean right = isValidBST(root.right);
return left && right;
}
}
思路三:双指针 前一个节点与后一个节点进行比较
class Solution {
//用来记录一个前节点
TreeNode pre = null;
public boolean isValidBST(TreeNode root) {
if(root == null){
return true;
}
boolean left = isValidBST(root.left);
if(pre != null && pre.val >= root.val){
return false;
}
pre = root;
boolean right = isValidBST(root.right);
return left && right;
}
}