654.最大二叉树
题目链接:https://leetcode.com/problems/maximum-binary-tree/
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
二叉树的根是数组中的最大元素。
左子树是通过数组中最大值左边部分构造出的最大二叉树。
右子树是通过数组中最大值右边部分构造出的最大二叉树。
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
if (nums.length == 0){
return null;
}
int index = 0;
int maxVal = nums[0];
for (int i = 0; i < nums.length; i++){
if (nums[i] > maxVal) {
index = i;
maxVal = nums[i];
}
}
TreeNode root = new TreeNode(maxVal);
// if (nums.length == 1){
// return root;
// }
int[] left_prefix = Arrays.copyOfRange(nums, 0, index);
int[] right_prefix = Arrays.copyOfRange(nums, index+1, nums.length);
root.left = constructMaximumBinaryTree(left_prefix);
root.right = constructMaximumBinaryTree(right_prefix);
return root;
}
}
思路:
- 与昨天的 用中序后序遍历构造二叉树 相似
- 构造树一般采用前序遍历,因为先构造中间节点,再左右子树。
- Arrays. copyOfRange
- 方法二:用数组构造二叉树的题目,每次分隔尽量不要定义新的数组,而是通过下标索引直接在原数组上操作,这样可以节约时间和空间上的开销。(没写)
617.合并二叉树
题目链接:https://leetcode.com/problems/merge-two-binary-trees/
思路:
- 本题使用哪种遍历都是可以的!
- 采用前序
方法一:递归法
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
//终止条件
if (root1 == null && root2 == null) {
return null;
} else if (root1 == null && root2!= null){
return root2;
} else if (root1 != null && root2 ==null) {
return root1;
}
// 或者 终止条件的简化写法
if(root1 == null) return root2;
if(root2 == null) return root1;
TreeNode root = new TreeNode(root1.val + root2.val); // 中
root.left = mergeTrees(root1.left, root2.left); // 左
root.right = mergeTrees(root1.right, root2.right); // 右
return root;
}
}
方法二:迭代法
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
Deque<TreeNode> queue = new LinkedList<>();
if (root1 == null) return root2;
if (root2 == null) return root1;
queue.offer(root1);
queue.offer(root2);
while (!queue.isEmpty()) {
// 为啥没有层序遍历的二层循环?
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
node1.val += node2.val; //两个节点不为空,直接相加替换root1的值
if (node1.left != null && node2.left != null) {
queue.offer(node1.left);
queue.offer(node2.left);
}
if (node1.right != null && node2.right != null) {
queue.offer(node1.right);
queue.offer(node2.right);
}
if (node1.left == null) { // 为空就直接赋值替代
node1.left = node2.left;
}
if (node1.right == null){
node1.right = node2.right;
}
}
return root1;
}
}
思路:
- 递归法:前序遍历
- 迭代法:使用队列模拟类似层序遍历,同时处理两个树的节点
700. 二叉搜索树中的搜索
题目链接:https://leetcode.com/problems/search-in-a-binary-search-tree/
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
思路:
二叉搜索树是一个有序树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉搜索树
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
// 递归法
if (root == null) return null;
if (root.val == val) return root;
TreeNode result = null;
if (root.val > val) {
result = searchBST(root.left, val);
}
if (root.val < val) {
result = searchBST(root.right, val);
}
return result;
}
}
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
// 迭代法
while (root != null) {
if (root.val > val) {
root = root.left;
} else if (root.val < val) {
root = root.right;
} else {
return root;
}
}
return null;
}
}
98.验证二叉搜索树
题目链接:https://leetcode.com/problems/validate-binary-search-tree/
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
思路:
-
采用中序遍历,此时输出的二叉搜索树节点的数值是有序序列。
-
验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。
-
陷阱1: 我们要比较的是 左子树所有节点小于中间节点,右子树所有节点大于中间节点
陷阱2: 最小值可能是int的最小值,所以用long.min -
返回值(bool): 我们在寻找一个不符合条件的节点,如果没有找到这个节点就遍历了整个树,如果找到不符合的节点了,立刻返回。
if (root->val > root->left->val && root->val < root->right->val) {
return true;
} else {
return false;
}
方法一:递归法,设全局变量最小值,注意最小值初始化
class Solution {
long maxVal = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
boolean left = isValidBST(root.left);
if (root.val > maxVal) {
maxVal = root.val;
} else {
return false;
}
boolean right = isValidBST(root.right);
return left && right;
}
}
方法二:递归法,优化,双指针(pre和root/curr)重要
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; // 注意!!!此时pre开始后移
boolean right = isValidBST(root.right);
return left && right;
}
}
方法三:迭代法,中序遍历,栈
class Solution {
public boolean isValidBST(TreeNode root) {
// 迭代法,双指针
Deque<TreeNode> stack = new LinkedList<>();
if (root == null) return true;
TreeNode curr = root;
TreeNode pre = null;
while (!stack.isEmpty() || curr != null) {
if (curr != null) {
stack.push(curr);
curr = curr.left; // 左
} else {
curr = stack.pop();
if (pre != null && pre.val >= curr.val) {// 中
return false;
}
pre = curr;
curr = curr.right; // 右,注意没有push进stack
}
}
return true;
}
}