二叉树的前序遍历
class Solution {
List<Integer> x=new LinkedList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root==null) return x;
x.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return x;
}
}
二叉搜索树中的搜索
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root==null) return null;
if(root.val>val)
{
return searchBST(root.left,val);
}
else if(root.val<val)
{
return searchBST(root.right,val);
}
else
{
return root;
}
}
}
二叉搜索树中的插入
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root==null) return new TreeNode(val);
if(root.val<val)
{
root.right=insertIntoBST(root.right,val);
}
else
{
root.left=insertIntoBST(root.left,val);
}
return root;
}
}
BiNode
class Solution {
TreeNode prenode=null;
int k=0;
TreeNode t;
public TreeNode convertBiNode(TreeNode root) {
if(root==null) return null;
if(root.left==null&&root.right==null) return root;
cal(root);
return t;
}
public TreeNode cal(TreeNode root)
{
if(root==null) return null;
cal(root.left);
TreeNode x=new TreeNode(root.val);
if(prenode!=null)
{
prenode.right=x;
k++;
if(k==1)
{
t=prenode;
}
}
prenode=x;
cal(root.right);
return root;
}
}
层数最深叶子节点的和
class Solution {
public int deepestLeavesSum(TreeNode root) {
List<List<Integer>> sum=level(root);
for(List<Integer> x : sum)
{
int m=0;
for(int i : x)
{
m=m+i;
}
return m;
}
return 0;
}
public List<List<Integer>> level(TreeNode root)
{
List<List<Integer>> sum=new LinkedList<>();
if(root==null) return sum;
Queue<TreeNode> queue=new LinkedList<>();
queue.add(root);
while(!queue.isEmpty())
{
List<Integer> x=new LinkedList<>();
TreeNode temp;
int t=queue.size();
for(int i=0;i<t;i++)
{
temp=queue.poll();
x.add(temp.val);
if(temp.left!=null) queue.add(temp.left);
if(temp.right!=null) queue.add(temp.right);
}
sum.add(x);
}
Collections.reverse(sum);
return sum;
}
}
最大二叉树
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if(root==null)
{
return new TreeNode(val);
}
if(root.val<val)
{
TreeNode t=new TreeNode(val);
t.left=root;
return t;
}
else
{
root.right=insertIntoMaxTree(root.right,val);
return root;
}
}
}
二叉搜索树第k小的元素
class Solution {
List<Integer> sum=new LinkedList<>();
public int kthSmallest(TreeNode root, int k) {
cal(root);
return sum.get(k-1);
}
public void cal(TreeNode root)
{
if(root==null) return;
cal(root.left);
sum.add(root.val);
cal(root.right);
}
}
回文链表
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> m=new LinkedList<>();
List<Integer> n=new LinkedList<>();
while(head.next!=null)
{
m.add(head.val);
head=head.next;
}
m.add(head.val);
n.addAll(m);
Collections.reverse(n);
return m.equals(n);
}
}
删除链表中的节点
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
四的幂
class Solution {
public boolean isPowerOfFour(int n) {
int x=0;
while(Math.pow(4,x)<=n)
{
if(Math.pow(4,x)==n)
{
return true;
}
x++;
}
return false;
}
}
反转字符串
class Solution {
public void reverseString(char[] s) {
char temp;
for (int i=0;i<s.length/2;i++){
temp=s[i];
s[i] = s[s.length-1-i];
s[s.length-1-i] = temp;
}
}
}
将有序数组转化成二叉搜索树
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return cal(nums,0,nums.length-1);
}
public TreeNode cal(int[] nums,int l,int r)
{
if(r<l) return null;
int mid = l+((r-l)>>1); //求中点不要用 int mid = (l + r)/2,有溢出风险,稳妥的方法是 int mid = l + (r-l)/2
//mid=(left+right)>>1的含义右移运算符>>,运算结果正好能对应一个整数的二分之一值,这就正好能代替数学上的除2运算,但是比除2运算要快。
mid=(left+right)>>1相当于mid=(left+right)/2
TreeNode root=new TreeNode(nums[mid]);
root.left=cal(nums,l,mid-1);
root.right=cal(nums,mid+1,r);
return root;
}
}
动态规划
509.斐波那契数
70.爬楼梯
746.使用最小花费爬楼梯
62.不同路径
63.不同路径II
343.整数拆分
96.不同的二叉搜索树