144. 二叉树的前序遍历
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
示例 1:
输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[1,2]
示例 5:
输入:root = [1,null,2]
输出:[1,2]
解析过程:
二叉树的前序遍历算法顺序:根-左-右
采用递归算法。先访问 root 节点,然后将 root 节点的值加入存储答案的集合中,再递归调用 方法来遍历 root 节点的左子树,以及root 节点的右子树,直到碰到空节点,递归终止。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
//DFS
List<Integer> node =new ArrayList<>();
PreOrder(root,node);
return node;
}
public void PreOrder(TreeNode root,List<Integer> node){
if(root == null){
return;
}
node.add(root.val);
PreOrder(root.left,node);
PreOrder(root.right,node);
}
}
调试:
import java.util.List;
public class Main {
public static void main(String[] args) {
System.out.println("----递归------");
System.out.println("前序:");
TreeNode treeNode = new TreeNode(1, null, new TreeNode(2, new TreeNode(3, null, null), null));
List<Integer> list = new Leetcode144().preorderTraversal(treeNode);
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i));
}
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:36.8 MB, 在所有 Java 提交中击败了22.27%的用户
通过测试用例:69 / 69
94. 二叉树的中序遍历
给定一个二叉树的根节点 root ,返回它的 中序 遍历。
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[2,1]
示例 5:
输入:root = [1,null,2]
输出:[1,2]
解析过程:
二叉树的中序遍历算法顺序:左-根-右
采用递归算法。先递归调用方法遍历root节点的左子树,然后将root节点的值加入存储答案的集合中,再递归调用方法遍历root节点的右子树,直到碰到空节点,递归终止。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//中序遍历:左-根-右
List<Integer> node=new ArrayList<>();
InOrder(root,node);
return node;
}
public void InOrder(TreeNode root,List<Integer>node){
if(root == null){
return;
}
InOrder(root.left,node);
node.add(root.val);
InOrder(root.right,node);
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:36.7 MB, 在所有 Java 提交中击败了53.21%的用户
通过测试用例:68 / 68
145. 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [3,2,1]
解析过程:
二叉树的后序遍历算法顺序:左-右-根
采用递归算法。先递归调用方法遍历root节点的左子树,然后递归调用方法遍历root节点的右子树,最后将root节点的值加入存储答案的集合中,直到碰到空节点,递归终止。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
//后序遍历:左-右-根
List<Integer> node=new ArrayList<>();
PostOrder(root,node);
return node;
}
public void PostOrder(TreeNode root,List<Integer>node){
if(root == null){
return;
}
PostOrder(root.left,node);
PostOrder(root.right,node);
node.add(root.val);
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:36.6 MB, 在所有 Java 提交中击败了68.43%的用户
通过测试用例:68 / 68
102. 二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其层序遍历结果:
[
[3],
[9,20],
[15,7]
]
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
//层次遍历,类似于BFS,需要设置一个队列
Deque<TreeNode> queue=new ArrayDeque<>();
// Queue<TreeNode> queue=new LinkedList<>(); 两种都可以
//存储层序遍历的结果
List<List<Integer>> list=new ArrayList<>();
//如果节点不为空,则加入队列
if(root !=null){
queue.add(root);
}
while(!queue.isEmpty()){
int len=queue.size();
List<Integer> level=new ArrayList<>();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
level.add(node.val);
if(node.left !=null){
queue.add(node.left);
}
if(node.right !=null){
queue.add(node.right);
}
}
list.add(level);
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了90.91%的用户
内存消耗:38.8 MB, 在所有 Java 提交中击败了17.52%的用户
通过测试用例:34 / 34
103. 二叉树的锯齿形层序遍历
给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回锯齿形层序遍历如下:
[
[3],
[20,9],
[15,7]
]
解析过程:
方法一:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//层序遍历
//此处需要一个双端队列,对当前层节点的存储需要设置一个flag记录是从左至右还是从右至左的
//从左至右:将遍历到的节点插入双端队列的末尾 offerLast()方法用于在此双端队列的最后添加给定元素
//从右至左:将遍历到的节点插入双端队列的头部 offerFirst()方法用于将给定元素添加到此双端队列的前面
Queue<TreeNode> queue=new LinkedList<>();
List<List<Integer>> list=new ArrayList<>();
if(root != null){
queue.offer(root);
}
boolean flag=true;
while(! queue.isEmpty()){
int len=queue.size();
Deque<Integer> level=new ArrayDeque<>();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
if(flag){
level.offerLast(node.val);
}else {
level.offerFirst(node.val);
}
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
list.add(new ArrayList<Integer> (level));
flag=!flag;
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了93.45%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了10.88%的用户
通过测试用例:33 / 33
方法二:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//层序遍历
//此处需要一个双端队列,对当前层节点的存储需要设置一个count计数,如果count%2==0,则从左至右加入,若count%2==1,则从右至左加入
//从左至右:将遍历到的节点插入双端队列的末尾 offerLast()方法用于在此双端队列的最后添加给定元素
//从右至左:将遍历到的节点插入双端队列的头部 offerFirst()方法用于将给定元素添加到此双端队列的前面
Queue<TreeNode> queue=new LinkedList<>();
List<List<Integer>> list=new ArrayList<>();
if(root != null){
queue.offer(root);
}
int count=0;
while(! queue.isEmpty()){
int len=queue.size();
Deque<Integer> level=new ArrayDeque<>();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
if(count%2==0){
level.offerLast(node.val);
}else {
level.offerFirst(node.val);
}
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
list.add(new ArrayList<Integer> (level));
count++;
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了93.45%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了21.69%的用户
通过测试用例:33 / 33
107. 二叉树的层序遍历 II
给定一个二叉树,返回其节点值自底向上的层序遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其自底向上的层序遍历为:
[
[15,7],
[9,20],
[3]
]
解析过程:
方法一:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
//在输出的时候,将当前层的元素加入最前面
List<List<Integer>> list=new ArrayList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root !=null){
queue.offer(root);
}
while(! queue.isEmpty()){
int len=queue.size();
List<Integer> level=new ArrayList<>();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
level.add(node.val);
if(node.left!=null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
//add(int index,E element) :在给定索引处插入元素。 给定索引中的元素将移至列表的右侧。 如果给定索引超出范围,则该方法将抛出IndexOutOfBoundsException。
list.add(0,level);
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了94.66%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了25.79%的用户
通过测试用例:34 / 34
方法二:
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list=new ArrayList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root !=null){
queue.offer(root);
}
while(! queue.isEmpty()){
int len=queue.size();
List<Integer> level=new ArrayList<>();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
level.add(node.val);
if(node.left!=null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
list.add(level);
}
//反向输出list
List temp;
for(int i=0,j=list.size()-1;i<list.size()/2;i++,j--){
temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了94.66%的用户
内存消耗:38.4 MB, 在所有 Java 提交中击败了85.05%的用户
通过测试用例:34 / 34
104. 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
//DFS
//分别记录左子树和右子树的高度,最后两者中最大的数+1就是最大深度
if(root == null){
return 0;
}else{
int leftHeight=maxDepth(root.left);
int rightHeight=maxDepth(root.right);
return Math.max(leftHeight,rightHeight)+1;
}
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.6 MB, 在所有 Java 提交中击败了5.18%的用户
通过测试用例:39 / 39
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
int depth=Integer.MAX_VALUE;
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 1;
}
//调用Math()中的min()方法,比较每个从根节点到最近叶子节点的最短路径
if(root.left!=null){
depth=Math.min(minDepth(root.left),depth);
}
if(root.right!=null){
depth=Math.min(minDepth(root.right),depth);
}
return depth+1;
}
}
结果:
执行用时:6 ms, 在所有 Java 提交中击败了52.88%的用户
内存消耗:58.7 MB, 在所有 Java 提交中击败了49.11%的用户
通过测试用例:52 / 52
101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//格式一
class Solution {
public boolean isSymmetric(TreeNode root) {
//设置一个队列存入二叉树节点,每次从队首退出两个值,判断是否一样
Queue<TreeNode> queue=new LinkedList<>();
if(root == null){
return true;
}
//初始化队列,将根节点加入两次
queue.offer(root);
queue.offer(root);
while(! queue.isEmpty()){
TreeNode node1=queue.poll();
TreeNode node2=queue.poll();
if(node1 == null && node2 == null){
continue;
}
if((node1 == null || node2 == null) || (node1.val != node2.val)){
return false;
}
queue.offer(node1.left);
queue.offer(node2.right);
queue.offer(node1.right);
queue.offer(node2.left);
}
return true;
}
}
//格式二
class Solution {
public boolean isSymmetric(TreeNode root) {
//迭代
return Symmetry(root,root);
}
public boolean Symmetry(TreeNode n1,TreeNode n2){
//设置一个队列,每次将左子树的左节点和右子树的右节点,以及左子树的右节点和右子树的左节点加入队列
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(n1);
queue.offer(n2);
while(! queue.isEmpty()){
n1=queue.poll();
n2=queue.poll();
if(n1==null && n2 ==null ){
continue;
}
if((n1 ==null || n2==null)||(n1.val != n2.val)){
return false;
}
queue.offer(n1.left);
queue.offer(n2.right);
queue.offer(n1.right);
queue.offer(n2.left);
}
return true;
}
}
-------------------------------------------------
class Solution {
public boolean isSymmetric(TreeNode root) {
//递归
return Symmetry(root,root);
}
public boolean Symmetry(TreeNode n1,TreeNode n2){
if(n1 ==null && n2==null){
return true;
}
if((n1==null || n2==null)|| (n1.val !=n2.val)){
return false;
}
return Symmetry(n1.left,n2.right)&&Symmetry(n1.right,n2.left);
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了21.66%的用户
内存消耗:37.7 MB, 在所有 Java 提交中击败了13.74%的用户
通过测试用例:197 / 197
226. 翻转二叉树
翻转一棵二叉树。
示例:
输入:
4
/
2 7
/ \ /
1 3 6 9
输出:
4
/
7 2
/ \ /
9 6 3 1
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
//递归,先交换一下左右节点,然后再递归的交换左节点,右节点
//递归函数的终止条件,节点为空时返回
if(root == null){
return null;
}
//当前节点的左右子树进行交换
TreeNode temp=null;
temp=root.left;
root.left=root.right;
root.right=temp;
//递归
TreeNode left=invertTree(root.left);
TreeNode right=invertTree(root.right);
return root;
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:35.9 MB, 在所有 Java 提交中击败了54.20%的用户
通过测试用例:77 / 77
112. 路径总和
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。
叶子节点 是指没有子节点的节点。
提示:
树中节点的数目在范围 [0, 5000] 内
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:false
示例 3:
输入:root = [1,2], targetSum = 0
输出:false
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
//递归 DFS
return DFS(root,0,targetSum);
}
public boolean DFS(TreeNode root,int value,int targetSum){
if(root ==null){
return false;
}
value +=root.val;
if(root.left ==null && root.right == null){
return value == targetSum;
}else{
return DFS(root.left,value,targetSum) ||DFS(root.right,value,targetSum);
}
}
}
结果:
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.3 MB, 在所有 Java 提交中击败了59.60%的用户
通过测试用例:117 / 117
113. 路径总和 II
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:[]
示例 3:
输入:root = [1,2], targetSum = 0
输出:[]
解析过程:
使用LinkedList<>()
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Deque<Integer> array=new LinkedList<>();
List<List<Integer>> list=new LinkedList<>();//存储所有满足条件的路径
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
AllPath(root,0,targetSum);
return list;
}
public void AllPath(TreeNode root,int value,int targetSum){
if(root == null){
return;
}
array.offerLast(root.val);
value += root.val;
//首先需要满足该路径是从根节点到叶子结点的路径总和等于给定目标和的路径
if(root.left == null && root.right == null && value == targetSum){
list.add(new LinkedList<Integer>(array));
}
AllPath(root.left,value,targetSum);
AllPath(root.right,value,targetSum);
array.pollLast();
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.8 MB, 在所有 Java 提交中击败了34.11%的用户
通过测试用例:115 / 115
使用ArrarList<>()
class Solution {
List<Integer> array=new ArrayList<>();
List<List<Integer>> list=new ArrayList<>();//存储所有满足条件的路径
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
AllPath(root,0,targetSum);
return list;
}
//首先需要满足该路径是从根节点到叶子结点的路径总和等于给定目标和的路径
public void AllPath(TreeNode root,int value,int targetSum){
if(root == null){
return;
}
array.add(root.val);
value += root.val;
if(root.left == null && root.right == null && value == targetSum){
list.add(new ArrayList<Integer>(array));
}
AllPath(root.left,value,targetSum);
AllPath(root.right,value,targetSum);
array.remove(array.size()-1);
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了64.49%的用户
通过测试用例:115 / 115
437. 路径总和 III
给定一个二叉树的根节点 root ,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum 的 路径 的数目。
路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
示例 1:
输入:root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
输出:3
解释:和等于 8 的路径有 3 条,如图所示。
示例 2:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:3
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int pathSum(TreeNode root, int targetSum) {
//对二叉树上每个节点 p 求出DFS(p,targetSum),然后对这些路径数目求和即为返回结果。
if(root == null){
return 0;
}
int sum=DFS(root,targetSum);
sum += pathSum(root.left,targetSum);
sum += pathSum(root.right,targetSum);
return sum;
}
//DFS(p,val):以节点 p 为起点向下且满足路径总和为 val 的路径数目
public int DFS(TreeNode root,int targetSum){
if(root == null){
return 0;
}
int count=0;
int value=root.val;
if(value == targetSum){
count++;
}
count+=DFS(root.left,targetSum-value);
count+=DFS(root.right,targetSum-value);
return count;
}
}
结果:
执行用时:27 ms, 在所有 Java 提交中击败了24.74%的用户
内存消耗:38.3 MB, 在所有 Java 提交中击败了27.96%的用户
通过测试用例:126 / 126
257. 二叉树的所有路径
给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
提示:
树中节点的数目在范围 [1, 100] 内
-100 <= Node.val <= 100
示例 1:
输入:root = [1,2,3,null,5]
输出:[“1->2->5”,“1->3”]
示例 2:
输入:root = [1]
输出:[“1”]
解析过程:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> list=new ArrayList<>();//存储路径
BT(root,"",list);
return list;
}
public List<String> BT(TreeNode root,String path, List<String> list){
if(root != null){
//和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象
StringBuffer newpath=new StringBuffer(path);
//Integer.toString()方法 该方法返回指定整数的有符号位的String对象,以10进制字符串形式返回
newpath.append(Integer.toString(root.val));
if(root.left == null && root.right == null){
//若当前节点为叶子节点,则将其加入list中
list.add(newpath.toString());
}else {
newpath.append("->");
//当前节点不是叶子节点,继续递归遍历
BT(root.left,newpath.toString(),list);
BT(root.right,newpath.toString(),list);
}
}
return list;
}
}
结果:
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.4 MB, 在所有 Java 提交中击败了82.97%的用户
通过测试用例:208 / 208