Leecodde235 二叉搜索树的最近公共祖先
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return null;
TreeNode re_node = null;
if(p.val<root.val&&q.val<root.val)
re_node = lowestCommonAncestor(root.left,p,q);
else if(p.val>root.val&&q.val>root.val)
re_node = lowestCommonAncestor(root.right,p,q);
else
re_node = root;
return re_node;
}
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null) return null;
if(p.val<root.val&&q.val<root.val)
return lowestCommonAncestor(root.left,p,q);
if(p.val>root.val&&q.val>root.val)
return lowestCommonAncestor(root.right,p,q);
return root;
}
}
Leecode98 验证一棵树是否是二叉搜索树
乍得一看,这题思路很简单,似乎就是判断当前节点是否满足二叉搜索树的定义。然后判断左子树是否满足,判断右子树是否满足。是一个典型的二叉树递归思想。代码如下:
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
if(root.left!=null&&root.right!=null){
if(root.left.val>=root.val||root.right.val<=root.val)
return false;
}
if(root.left!=null){
if(root.left.val>=root.val)
return false;
}
if(root.right!=null){
if(root.right.val<=root.val)
return false;
}
if(isValidBST(root.left)&&isValidBST(root.right))
return true;
return false;
}
但是抱歉,提交后返回false结果。这是为什么呢,看下面这个测试用例:
我们会发现左子树满足,右子树满足。但是根节点与右子树中的数据发生冲突,所以不能简单的判断左子树是二叉搜索树,右子树是二叉搜索树,那么当前root就是二叉搜索树。此时需要记录上下界,以进行进一步的判断。
为左子树设置上界,当前节点若超过上界,则发生错误;
为右子树设置下界,若当前节点小于下界,则发生错误;
注意:在判断节点值大小时,不要忽略了=的情况。
public boolean isValidBST(TreeNode root) {
return heper(root,null,null);
}
public boolean heper(TreeNode root,Integer lower,Integer upper){
if(root==null) return true;
int val = root.val;
if(lower!=null&&val<=lower) return false;
if(upper!=null&&val>=upper) return false;
if(!heper(root.left,lower,val))
return false;
if(!heper(root.right,val,upper))
return false;
return true;
}
其实就是删除二叉搜索树的操作(上一篇文章中提到过,直接上代码):
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
if(root.val<key) root.right = deleteNode(root.right,key);
else if(root.val>key) root.left = deleteNode(root.left,key);
else{
if(root.left==null) return root.right;
else if(root.right==null) return root.left;
else{
TreeNode rootcopy = root;
root = findMin(rootcopy.right);
root.right = deleteMin(rootcopy.right);
root.left = rootcopy.left;
}
}
return root;
}
public TreeNode findMin(TreeNode root){
if(root==null) return null;
TreeNode pre = root;
while(root!=null){
pre = root;
root = root.left;
}
return pre;
}
public TreeNode deleteMin(TreeNode root){
if(root == null) return root;
if(root.left==null) return root.right;
root.left = deleteMin(root.left);
return root;
}
}
Leecode108 将有序数组转换为二叉搜索树
先明确几个知识点:
1.先序遍历+中序遍历,可以唯一确定一棵二叉树
2.后序遍历+中序遍历,可以唯一确定一棵二叉树
二叉搜索树的有序数据为二叉搜索树的中序遍历结果,本题的言外之意就是:给定二叉搜索树的先序遍历,给出这棵二叉树。
明确:该树的结果不唯一,奇数总能找到中间节点,而对于偶数找左边的节点作为根还是找右边的节点作为根是不一样的构建方法。
偶数找左边的节点作为根:
public TreeNode sortedArrayToBST(int[] nums) {
TreeNode root = createBST(nums,0,nums.length-1);
return root;
}
public TreeNode createBST(int[] nums, int left,int right){
if(left>right) return null;
int p = (left+right)/2;
TreeNode root = new TreeNode(nums[p]);
root.left = createBST(nums,left,p-1);
root.right = createBST(nums,p+1,right);
return root;
}
当为偶数p+1,找右边的节点作为根
public TreeNode sortedArrayToBST(int[] nums) {
TreeNode root = createBST(nums,0,nums.length-1);
return root;
}
public TreeNode createBST(int[] nums, int left,int right){
if(left>right) return null;
int p = (left+right)/2;
//节点的个数是偶数个,从0开始的
if((left+right)%2==1)
p++;
TreeNode root = new TreeNode(nums[p]);
root.left = createBST(nums,left,p-1);
root.right = createBST(nums,p+1,right);
return root;
}
还可以随机选取左右作为根节点:
public TreeNode sortedArrayToBST(int[] nums) {
TreeNode root = createBST(nums,0,nums.length-1);
return root;
}
public TreeNode createBST(int[] nums, int left,int right){
if(left>right) return null;
int p = (left+right)/2;
if ((left + right) % 2 == 1)
p += rand.nextInt(2);//0和1随机取一个相加
TreeNode root = new TreeNode(nums[p]);
root.left = createBST(nums,left,p-1);
root.right = createBST(nums,p+1,right);
return root;
}
Leecode230 二叉搜索树中第K个元素
根据二叉搜索树的特点,很容易想到通过递归实现,其思想如下
1.中序遍历二叉搜索树得到一个有序的集合
2.获取集合中第k个元素即可
递归:
class Solution {
List<Integer> list = new ArrayList<Integer>();
public int kthSmallest(TreeNode root, int k) {
inorder(root);
return list.get(k-1);
}
public void inorder(TreeNode root){
if(root==null) return;
inorder(root.left);
list.add(root.val);
inorder(root.right);
}
}
观察二叉搜索树特点,从最小的数据开始存入取出,判断是否为第k个。不满足再把较大的放进去,再取出判断…
1.将根节点所有最左边的分支存入栈,保证了栈pop是从最小开始输出
2.一次出一个栈,判断是否为第k个元素,是返回不是执行3
3.出一次栈,就要补充当前出栈元素的右子树下的数值(比栈中下一个元素小的所有元素,因为栈中下一个元素是当前节点的根节点,一定比当前节点的左右子树节点数值都大)。
迭代123,直到找到第k个元素。这种方式,边遍历边查找,不需要像递归一样先全部遍历后再查找。
迭代:
class Solution {
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while(true){
while(root!=null){
stack.add(root);
root = root.left;
}
TreeNode node = stack.pop();
if(--k==0) return node.val;
root = node.right;
}
}
}
Leecode236 二叉树最近祖先LCA
Leecode官方解析,动画很详细易懂
观察树,从下到上进行标记。一个节点存在三个状态:
1.当前节点就是p或者q,标记为true
2.当前节点的左边有p或者q,标记为true
3.当前节点的右边有p或者q,标记为true
将当前节点的返回值相加,只要出现两个true就代表当前节点就是根节点。
class Solution {
TreeNode ans = null;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
helper(root,p,q);
return ans;
}
public boolean helper(TreeNode node ,TreeNode p,TreeNode q){
if(node ==null) return false;
int left = helper(node.left,p,q)?1:0;
int right = helper(node.right,p,q)?1:0;
int mid = (node.val==p.val||node.val==q.val)?1:0;
if(left+right+mid>=2)
ans = node;
return (left+right+mid>0);
}
}