在这里插入代码片
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
public boolean hasPathSum (TreeNode root, int sum) {
// write code here
if(root==null){return false;}
if(root.left==null&&root.right==null&&sum-root.val==0){
return true;
}
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
}
}
在这里插入代码片
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode newhead=new ListNode(-1);
ListNode cur=newhead;
if(list1==null){return list2;}
if(list2==null){return list1;}
while(list1!=null&&list2!=null){
if(list1.val<=list2.val){
cur.next=list1;
cur=list1;
list1=list1.next;
}else{
cur.next=list2;
cur=list2;
list2=list2.next;
}
}if(list1!=null ){cur.next=list1;}
if(list2!=null){cur.next=list2;}
return newhead.next;
}
}
描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public TreeNode head=null;
public TreeNode prev=null;
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree==null){
return null;
}
Convert(pRootOfTree.left);
if(prev==null){
head=pRootOfTree;
prev=pRootOfTree;
}else{
prev.right=pRootOfTree;
pRootOfTree.left=prev;
prev=pRootOfTree;
}
Convert(pRootOfTree.right);
return head;}
}