leetcode 236 二叉树的最近公共祖先
本题采用后续遍历,若上一层递归函数中的传入下一层函数的左子树与右子树的返回值都是true,则说明本层的节点是目标节点的公共祖先(目标节点存在于其左右子树之中)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//采用后序遍历
//定义递归的终止条件
if(root == null){return null;}
if(root == p || root == q){
return root;
}
//遍历左子树
TreeNode left = lowestCommonAncestor(root.left, p, q);
//遍历右子树
TreeNode right = lowestCommonAncestor(root.right, p , q);
//根据左右子树的结果来判断当前递归层中的节点是否是p q的公共祖先
if(left != null && right != null){
return root;
}
if(left == null && right != null){
return right;
}
else if(left != null && right ==null){
return left;
}else{
return null;
}
}
}
leetcode 235 二叉搜索树的最近公共祖先
本题也采用后序遍历,注意利用二叉搜索树的排序性质即可。
注意:后序遍历适合于解决需要利用前面两个递归函数的返回值的情况。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//递归的终止条件
if(root == null){
return null;
}
if(root == p || root == q){
return root;
}
//单层递归逻辑
if(p.val < root.val && q.val < root.val){
TreeNode left = lowestCommonAncestor(root.left, p, q);
return left;
}
if(p.val > root.val && q.val > root.val){
TreeNode right = lowestCommonAncestor(root.right, p, q);
return right;
}else{
//p q 节点分布在根节点两侧,则根节点就是公共祖先
return root;
}
}
}
leetcode 701 二叉搜索树中的插入操作
插入的节点一定是连在原树的叶子节点上。
/**
* 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 insertIntoBST(TreeNode root, int val) {
//递归的终止条件
if(root == null){
TreeNode node = new TreeNode(val);
return node;
}
//单层递归逻辑
if(val < root.val){
root.left = insertIntoBST(root.left, val);
}else{
root.right = insertIntoBST(root.right, val);
}
return root;
}
}
leetcode 450 删除二叉搜索树中的节点
注意递归终止条件要分情况讨论:
1.删除节点就是叶子节点
2.删除节点只有左子树
3.删除节点只有右子树
4.删除节点拥有左右子树
/**
* 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 deleteNode(TreeNode root, int key) {
//递归的终止条件
if(root == null){
return null;
}
if(root.val == key){
//如果删除的节点是叶子节点
if(root.left == null && root.right == null){
return null;
}
//如果删除的节点的左子树不为空,右子树为空
else if(root.left != null && root.right == null){
return root.left;//返回的值的接受对象是上一层递归函数中对应的节点的左子树
}
else if(root.left == null && root.right != null){
return root.right;
}else{
//待删除节点的左右子树都不为空
//可以选择任意一颗子树连在删除节点的位置上。首先需要找到到左子树中的最大值(最右侧)或右子树的最小值(最左侧)
//再将其中的一颗子树连在相应的位置上(最左或最右)
//这里选择连接右子树
TreeNode cur = root.right;
while(cur.left != null){
cur = cur.left;
}
cur.left = root.left;
return root.right;
}
}
//定义单层递归
if(root.val < key){
root.right = deleteNode(root.right, key);
}else{
root.left = deleteNode(root.left, key);
}
return root;
}
}
leetcode 538 把二叉搜索树转换为累加树
本题采用右中左的遍历顺序。利用求累计数组的双指针的思想。
/**
* 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 int sum = 0;
public void travesal (TreeNode cur){
//递归终止条件
if(cur == null){
return;//因为递归函数的返回值是void,故这里可以return空
}
travesal(cur.right);
sum += cur.val;
cur.val = sum;
travesal(cur.left);
}
public TreeNode convertBST(TreeNode root) {
if(root == null){
return null;
}
travesal(root);
return root;
}
}
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 sortedArrayToBSTHelper(int[] nums, int left, int right){
if(left > right){
return null;
}
int mid = (left + right) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = sortedArrayToBSTHelper(nums, left, mid -1);
node.right = sortedArrayToBSTHelper(nums, mid + 1, right);
return node;
}
public TreeNode sortedArrayToBST(int[] nums) {
TreeNode root = sortedArrayToBSTHelper(nums, 0, nums.length - 1);
return root;
}
}
leetcode 669 修剪二叉搜索树
注意:删除二叉树中的一个节点,应将待删除节点的左子树或右子树连在待删除节点的父节点上。
/**
* 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 trimBST(TreeNode root, int low, int high) {
//递归的中止条件
if(root == null){
return null;
}
//处理单层递归逻辑
if(root.val < low){
return trimBST(root.right, low, high);//因为其右子树可能会有满足要求的节点
}
if(root.val > high){
return trimBST(root.left, low, high);//因为其左子树可能会有满足要求的节点
}
//当前递归层的节点不满足删除条件,则继续向下层递归。将递归结果返回给当前节点的左子树和右子树
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}