LeetCode109.有序链表转换成二叉搜索树
这道题是 LeetCode108.从有序数组中构造二叉查找树 的进阶版
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
return helper(new TreeNode(0),head,0,listlen(head)-1);
}
public TreeNode helper(TreeNode root,ListNode head,int s,int e){
if(s <= e){
int mid = s + (e - s)/2;
ListNode copy = head;
for(int i = 0;i < mid;i++){
copy = copy.next;
}
root = new TreeNode(copy.val);
root.left = helper(root.left,head,s,mid-1);
root.right = helper(root.right,copy.next,0,e-mid-1);
return root;
}
return null;
}
public int listlen(ListNode head){
int len = 0;
while(head != null){
len++;
head = head.next;
}
return len;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head == null){
return null;
}
if (head.next == null) return new TreeNode(head.val); //这个是必须的,否则就死循环了
ListNode slow = head;
ListNode fast = head;
ListNode pre = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
pre = slow;
slow = slow.next;
}
fast = slow.next;
pre.next = null;
TreeNode root = new TreeNode(slow.val);
root.left = sortedListToBST(head);
root.right = sortedListToBST(fast);
return root;
}
}
LeetCode653. 两数之和 IV - 输入 BST
这道题很简单,就先中序遍历序列到数组中,然后使用双指针找
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> data = new ArrayList<>();
inorder(data,root);
int p1 = 0,p2 = data.size()-1;
while(p1 < p2){
int sum = data.get(p1) + data.get(p2);
if(sum > k){
p2--;
}else if(sum < k){
p1++;
}else{
return true;
}
}
return false;
}
public void inorder(List<Integer> data,TreeNode root){
if(root == null){
return;
}
inorder(data,root.left);
data.add(root.val);
inorder(data,root.right);
}
}
LeetCode501.二叉搜索树中的众数
只需要在遍历的时候记录下几个参数值就行了
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int maxcount = 0;
TreeNode pre = null;
int count = 0;
public int[] findMode(TreeNode root) {
List<Integer> res = new LinkedList<Integer>();
inoder(root,res);
int[] koo = new int[res.size()];
for(int i = 0;i < res.size();i++){
koo[i] = res.get(i);
}
return koo;
}
public void inoder(TreeNode root,List<Integer> res){
if(root == null){
return;
}
inoder(root.left,res);
if(pre != null && root.val == pre.val){
count++;
}else{
count = 0;
}
if(maxcount < count){
res.clear();
res.add(root.val);
maxcount = count;
}else if(maxcount == count){
res.add(root.val);
}
pre = root;
inoder(root.right,res);
}
}