#LeetCode 654. Maximum Binary Tree
#LeetCode 654. 视频讲解:又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树_哔哩哔哩_bilibili
构造二叉树一般用前序遍历,是通过构造根节点再构造左子树、右子树实现。
递归三部曲:
1. 确定参数以及返回值,参数是int 数组,返回值是TreeNode 节点。
2. 确定终止条件,数组的长度是1 时。
3. 确定单层的递归逻辑。
首先找到数据中的最大值,然后根据最大值分割数组,
是否使用if 取决于终止条件,也可以删掉if 判断。
前序遍历代码:
/**
* 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) {
return construct(nums, 0, nums.length);
}
public TreeNode construct (int[] nums, int left, int right) {
if (right - left < 1) {
return null;
}
if (right - left == 1) {
return new TreeNode(nums[left]);
}
int maxValue = 0;
int index = 0;
for (int i = left; i < right; i++) {
if (maxValue < nums[i]) {
maxValue = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(maxValue);
if (index > 0) {
root.left = construct(nums, left, index);
}
if (index < nums.length - 1) {
root.right = construct(nums, index + 1, right);
}
return root;
}
}
#LeetCode 617. Merge Two Binary Trees
#LeetCode 617. 视频讲解:一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树_哔哩哔哩_bilibili
在这个题目中,前序遍历、中序遍历、后序遍历都可以用。
递归三部曲:
1. 确定递归的参数以及返回值:参数:TreeNode类型的两个根节点,返回值:合并后二叉树的根节点。
2. 确定终止条件:某一个树的节点为空时,或者同时为空。
if(root1 == null) { return root2;}
if (root2 == null) { return root1;}
可以包含两棵树任意一节点为空或者同时为空的情况,同时为空也是返回null。
3. 确定单层递归逻辑:把两棵树的元素加到一起。
递归(前序遍历)代码:
/**
* 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) {
if(root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
root1.val += root2.val;
root1.left = mergeTrees(root1.left, root2.left);
root1.right = mergeTrees(root1.right, root2.right);
return root1;
}
}
#LeetCode 700. Search in a Binary Search Tree
#LeetCode 700. 视频讲解:不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索_哔哩哔哩_bilibili
二叉搜索树的特点是:
若左子树不空,则左子树上所有结点的值均小于它的根结点的值,若右子树不空,则右子树上所有结点的值均大于它的根结点的值,它的左、右子树也分别为二叉搜索树。所以根据这个特点,不需要确定根据什么遍历的方法,只需要比较目标值与根节点的值。
递归法代码:
/**
* 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;
}
TreeNode result = null;
if (root.val < val) {
result = searchBST(root.right, val);
}
if (root.val > val) {
result = searchBST(root.left, val);
}
return result;
}
}
递归法代码:
/**
* 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) {
while(root != null) {
if (root.val < val) {
root = root.right;
}
else if (root.val > val) {
root = root.left;
}
else return root;
}
return null;
}
}
#LeetCode 98. Validate Binary Search Tree
#LeetCode 98. 视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树_哔哩哔哩_bilibili
设置了一个类似指针的pre,指向当前节点的前一个节点,目的是记录前一个节点的值,与当前节点比较。中序遍历是左中右,在二叉搜索树中,中序遍历中会呈现递增的序列,所以根据比较的结果返回是否是二叉搜索树的判断。
迭代法代码:
/**
* 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 {
TreeNode pre;
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;
}
}