687. 最长同值路径
一开始非常没有思路。看了解析以后,找到了思路,把长度看作是从一个节点向左右子树延伸出去的两个箭头,从下往上遍历,后序遍历递归,如果箭头上的值等于节点的值,那么那么路径的长度就加1。然后刚开始写出的代码分别计算两边的,然后加起来。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max=0;
public int longestUnivaluePath(TreeNode root) {
help(root);
return max;
}
public int help(TreeNode root){
if(root==null){
return 0;
}
int left=help(root.left);
int right=help(root.right);
if(root!=null&&root.left!=null){
if(root.val==root.left.val){
left++;
}else{
left=0;
}
}
if(root!=null&&root.right!=null){
if(root.val==root.right.val){
right++;
}else{
right=0;
}
}
max=Math.max(max,left+right);
return Math.max(left,right);
}
}
226. 翻转二叉树
递归。交换二叉树左右子树。然后对每一个左右子树进行递归。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
if(root.left!=null){
invertTree(root.left);
}
if(root.right!=null){
invertTree(root.right);
}
return root;
}
}
112. 路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
思路:参考剑指offer34,二叉树中和为某一值的路径。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path=new ArrayList<Integer>(); //每次都新建一个,题目没有让找出具体路径,只要有就返回true,找不到就返回false
public boolean hasPathSum(TreeNode root, int sum) {
findPath(root,sum);
if(path.size()==0){
return false;
}
return true;
}
public ArrayList<Integer> findPath(TreeNode root,int sum){
if(root==null){
return path;
}
// if((sum>0&&root.val>sum)||(sum<0&&root.val<sum)){
// return path;
// }
path.add(root.val);
sum-=root.val;
if(sum==0&&root.left==null&&root.right==null){
return path; //找到路径啦!
}
findPath(root.left,sum);
findPath(root.right,sum);
path.remove(path.size()-1);
return path;
}
}
113. 路径总和 II
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> pathlist=new ArrayList();
List<Integer> path=new ArrayList();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if(root==null){
return pathlist;
}
path.add(root.val);
sum-=sum=root.val;
if(sum==0&&root.left==null&&root.right==null){
pathlist.add(new ArrayList<>(path));
}
pathSum(root.left,sum);
pathSum(root.right,sum);
path.remove(path.size()-1);
return pathlist;
}
}
437. 路径总和 III
给定一个二叉树,它的每个结点都存放着一个整数值。
找出路径和等于给定数值的路径总数。
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int total=0;
ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path=new ArrayList<Integer>();
public int pathSum(TreeNode root, int sum) {
// int total;
if(root==null){
return 0;
}
total=numOfPath(root,sum);
//System.out.println(total+" "+root.val);
pathSum(root.left,sum);
//System.out.println(pathSum(root.left,sum)+"**");
pathSum(root.right,sum);
//System.out.println(pathSum(root.right,sum)+"**");
return total;
}
public int numOfPath(TreeNode root,int sum){ //对于树root的每一个节点来说,返回以这个节点为根节点的和为sum的路径个数
if(root==null){
return pathlist.size();
}
path.add(root.val);
sum-=root.val;
if(sum==0){
for(int i:path){
// System.out.print(i+" ");
}
//System.out.println();
pathlist.add(new ArrayList<>(path));
}
if(root.left!=null){
numOfPath(root.left,sum);
}
if(root.right!=null){
numOfPath(root.right,sum);
}
path.remove(path.size()-1);
//System.out.println(pathlist.size());
return pathlist.size();
}
}
未通过!
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int total=0;
ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path=new ArrayList<Integer>();
public int pathSum(TreeNode root, int sum) {
// int total;
if(root==null){
return total;
}
total=numOfPath(root,sum);
//System.out.println(total+" "+root.val);
pathSum(root.left,sum);
//System.out.println(pathSum(root.left,sum)+"**");
pathSum(root.right,sum);
//System.out.println(pathSum(root.right,sum)+"**");
return total;
}
public int numOfPath(TreeNode root,int sum){ //对于树root的每一个节点来说,返回以这个节点为根节点的和为sum的路径个数
if(root==null){
return pathlist.size();
}
path.add(root.val);
sum-=root.val;
if(sum==0){
//for(int i:path){
// System.out.print(i+" ");
//}
//System.out.println();
pathlist.add(path);
//return pathlist.size();
}
if(root.left!=null){
numOfPath(root.left,sum);
path.remove(path.size()-1);
}
if(root.right!=null){
numOfPath(root.right,sum);
path.remove(path.size()-1);
}
return pathlist.size();
}
}
109. 有序链表转换二叉搜索树
思路:先根据给的例子寻找思路!根据题意得,要求出得是平衡二叉搜索树,平衡二叉搜索树的条件是左右子树的高度差的绝对值不超过1!!所以对于单链表,我们从中间开始,找到mid位置!!先把左边变成一棵平衡二叉树返回left节点,再把右边变成一棵平衡二叉树,返回right节点,再和根连起来。
首先,寻找单链表的中间位置,设定两个指针,一个快指针,一个慢指针,当快指针的next为null时,慢指针指向中间位置。
/**
* 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);
}
if(head.next.next==null){
TreeNode root=new TreeNode(head.val);
root.right=new TreeNode(head.next.val);
return root;
}
//先找到list的中间位置
ListNode fast=head;
ListNode slow=head;
while(fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
//现在slow就是中间位置,slow前面的是left,slow后面的是right
ListNode tmp=head;
while(tmp.next!=slow){
tmp=tmp.next;
}
tmp.next=null;
ListNode left=head;
ListNode right=slow.next;
slow.next=null;//把链表从中间截断!!!!现在链表变成了left和right
TreeNode root=new TreeNode(slow.val);
TreeNode leftNode=sortedListToBST(left);
TreeNode rightNode=sortedListToBST(right);
if(leftNode!=null){
root.left=leftNode;
}
if(rightNode!=null){
root.right=rightNode;
}
return root;
}
}