努力了那么多年,回头一望,几乎全是漫长的挫折和煎熬。对于大多数人的一生来说,顺风顺水只是偶尔,挫折、不堪、焦虑和迷茫才是主旋律。我们登上并非我们所选择的舞台,演出并非我们所选择的剧本。继续加油吧!
目录
1、二叉树的前序遍历
题目链接:二叉树的前序遍历_牛客题霸_牛客网
思路:调用先序遍历函数将结果存入集合,再传入数组,先序遍历:根节点、左子树、右子树。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public void preOrder(TreeNode root, List<Integer> lst){
if(root == null){
return ;
}
lst.add(root.val) ;
preOrder(root.left, lst) ;
preOrder(root.right, lst) ;
}
public int[] preorderTraversal (TreeNode root) {
// write code here
List<Integer> lst = new ArrayList<>() ;
preOrder(root, lst) ;
int [] ans = new int [lst.size()] ;
int i = 0;
for(int num : lst){
ans[i++] = num ;
}
return ans ;
}
}
2、二叉树得中序遍历
题目链接:二叉树的中序遍历_牛客题霸_牛客网
思路:与先序思路一样,只不过是先左子树,再根节点,最后再右子树。
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
List<Integer> lst = new ArrayList<>() ;
inOrder(root, lst) ;
int i = 0 ;
int [] ans = new int [lst.size()] ;
for(int num : lst){
ans[i++] = num ;
}
return ans ;
}
public void inOrder(TreeNode root, List<Integer> lst){
if(root == null){
return ;
}
inOrder(root.left, lst) ;
lst.add(root.val) ;
inOrder(root.right, lst) ;
}
}
3、二叉树的后序遍历
题目链接:二叉树的后序遍历_牛客题霸_牛客网
思路:先左子树,再右子树,最后根节点。
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
// write code here
List<Integer> lst = new ArrayList<>() ;
postOrder(root, lst) ;
int i = 0 ;
int [] ans = new int [lst.size()] ;
for(int num : lst){
ans[i++] = num ;
}
return ans ;
}
public void postOrder(TreeNode root, List<Integer> arraylist){
if(root == null){
return ;
}
postOrder(root.left,arraylist) ;
postOrder(root.right,arraylist) ;
arraylist.add(root.val) ;
}
}
4、求二叉树的层序遍历
题目链接:求二叉树的层序遍历_牛客题霸_牛客网
思路:依靠队列先进先出的特性,每层元素全部出队,并存入集合,下一层全部入队。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> lst = new ArrayList<>() ;
Queue<TreeNode> queue = new LinkedList<>() ;
if(root == null){
return lst ;
}
queue.add(root) ;
while(!queue.isEmpty()){
int s = queue.size() ;
ArrayList<Integer> list = new ArrayList<>() ;
for(int i=0; i<s; i++){
TreeNode node = queue.poll() ;
list.add(node.val) ;
if(node.left != null){
queue.offer(node.left) ;
}
if(node.right != null){
queue.offer(node.right) ;
}
}
lst.add(list) ;
}
return lst ;
}
}
5、按之字形顺序打印二叉树
题目链接:按之字形顺序打印二叉树_牛客题霸_牛客网
思路:借助队列实现,每一层入队,并全部出队进入集合,如何是偶数层,则集合元素反转。
Java版:
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> lst = new ArrayList<>() ;
Queue<TreeNode> queue = new LinkedList<>() ;
if(pRoot == null){
return lst ;
}
queue.offer(pRoot) ;
int level = 1 ;
while(!queue.isEmpty()){
int size = queue.size() ;
ArrayList<Integer> list = new ArrayList<>() ;
for(int i=0; i<size; i++){
TreeNode node = queue.poll() ;
list.add(node.val) ;
if(node.left !=null){
queue.add(node.left) ;
}
if(node.right != null){
queue.add(node.right) ;
}
}
if(level%2==0){
Collections.reverse(list) ;
}
level ++ ;
lst.add(list) ;
}
return lst ;
}
}
6、二叉树的最大深度
题目链接:二叉树的最大深度_牛客题霸_牛客网
思路:借助队列,层次遍历,每一层入队,记录层数,然后出队。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
Queue<TreeNode> queue = new LinkedList<>();
if(root == null){
return 0 ;
}
int level = 0;
queue.offer(root) ;
while(!queue.isEmpty()){
level ++ ;
int size = queue.size() ;
for(int i=0; i<size; i++){
TreeNode node = queue.poll() ;
if(node.left != null){
queue.offer(node.left) ;
}
if(node.right != null){
queue.offer(node.right) ;
}
}
}
return level ;
}
}
7、 二叉树中和为某一值的路径(一)
思路:递归实现,每次递归遍历左右子树并累加当前节点值,如果满足条件,则返回true
Java版:
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 ;
}
int s = 0 ;
return preOrder(root, sum, s) ;
}
public boolean preOrder(TreeNode root, int sum, int s){
if(root == null){
return false ;
}
s += root.val ;
if(root.left == null && root.right == null && s==sum){
return true ;
}
return preOrder(root.left, sum, s) || preOrder(root.right, sum, s) ;
}
}
8、二叉搜索树与双向链表
题目链接:二叉搜索树与双向链表_牛客题霸_牛客网
思路:二叉搜索树,中序遍历有序,遍历过程依次修改指针,构建双向链表。
Java版:
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode root = null ;
TreeNode pre = null ;
public TreeNode Convert(TreeNode pRootOfTree) {
//二叉搜索树,也是二叉排序树,左子树小于根节点,右子树大于根节点
//如果中序遍历,则可以由小到大获取元素
if(pRootOfTree == null){
return null ;
}
Convert(pRootOfTree.left) ;
if(pre == null){
pre = pRootOfTree ;
root = pre ;
}else{
//修改双向指针,并更新当前节点为前驱节点
pre.right = pRootOfTree ;
pRootOfTree.left = pre ;
pre = pRootOfTree ;
}
Convert(pRootOfTree.right) ;
return root ;
}
}
9、对称的二叉树
题目链接:对称的二叉树_牛客题霸_牛客网
思路:迭代判断两个树的左子树和右子树是否一致。
Java版:
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
return f(pRoot, pRoot) ;
}
boolean f(TreeNode root1, TreeNode root2){
if(root1==null && root2 == null){
return true ;
}
if(root1 == null || root2 == null){
return false ;
}
if(root1.val != root2.val){
return false ;
}
return f(root1.left, root2.right) && f(root1.right, root2.left) ;
}
}
10、 合并二叉树
题目链接:合并二叉树_牛客题霸_牛客网
思路:递归合并二叉树,合并到左子树上,最后返回左子树。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param t1 TreeNode类
* @param t2 TreeNode类
* @return TreeNode类
*/
public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
// write code here
if(t1 == null){
return t2 ;
}
if(t2 == null){
return t1 ;
}
t1.val = t1.val + t2.val ;
if(t2.left != null){
t1.left = mergeTrees(t1.left, t2.left) ;
}
if(t2.right != null){
t1.right = mergeTrees(t1.right, t2.right) ;
}
return t1 ;
}
}
11、二叉树的镜像
题目链接:二叉树的镜像_牛客题霸_牛客网
思路:递归遍历左右子树,每次交换左右子树即可。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot == null){
return null ;
}
TreeNode tmp = pRoot.left ;
tmp = pRoot.left ;
pRoot.left = pRoot.right ;
pRoot.right = tmp ;
Mirror(pRoot.left) ;
Mirror(pRoot.right) ;
return pRoot ;
}
}
12、判断是不是二叉搜索树
题目链接:判断是不是二叉搜索树_牛客题霸_牛客网
思路:中序遍历,存值,然后依次判断是否元素是否依次增大即可。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isValidBST (TreeNode root) {
// write code here
ArrayList<Integer> list = new ArrayList<>() ;
inOrder(root, list) ;
for(int i=1; i<list.size(); i++){
if(list.get(i-1) >= list.get(i)){
return false ;
}
}
return true ;
}
public void inOrder(TreeNode root, ArrayList<Integer> list){
if(root == null){
return ;
}
inOrder(root.left, list) ;
list.add(root.val) ;
inOrder(root.right, list) ;
}
}
13、判断是不是完全二叉树
题目链接:判断是不是完全二叉树_牛客题霸_牛客网
思路:根据队列先进先出的原则,如果当前节点为空,后序节点必须都为空。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isCompleteTree (TreeNode root) {
// write code here
//当前节点为空,后面的节点必须全为空
Queue<TreeNode> queue = new LinkedList<>() ;
if(root == null){
return true ;
}
queue.add(root) ;
boolean flag = false ;
while(!queue.isEmpty()){
TreeNode node = queue.poll() ;
if(node == null){
flag = true ;
}else{
if(flag){
return false ;
}
queue.add(node.left) ;
queue.add(node.right) ;
}
}
return true ;
}
}
14、判断是不是平衡二叉树
题目链接:判断是不是平衡二叉树_牛客题霸_牛客网
思路:计算二叉树深度,判断左右子树深度之差是否大于1.
Java版:
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true ;
}
int left = depth(root.left) ;
int right = depth(root.right) ;
if(Math.abs(left - right) > 1){
return false ;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right) ;
}
public int depth(TreeNode root){
if(root == null){
return 0 ;
}
int left = depth(root.left) ;
int right = depth(root.right) ;
return Math.max(left, right) + 1 ;
}
}
15、二叉搜索树的最近公共祖先
思路:二叉搜索树满足中序遍历有序的性质,递归遍历,找到最近公共祖先。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param p int整型
* @param q int整型
* @return int整型
*/
public int lowestCommonAncestor (TreeNode root, int p, int q) {
// write code here
return find(root, p, q).val ;
}
public TreeNode find(TreeNode root, int p, int q) {
if ((p <= root.val && q >= root.val) || (p >= root.val && q <= root.val)) {
return root ;
} else if (p <= root.val && q <= root.val) {
return find(root.left, p, q) ;
}else{
return find(root.right, p, q) ;
}
}
}
16、在二叉树中找到两个节点的最近公共祖先
题目链接:在二叉树中找到两个节点的最近公共祖先_牛客题霸_牛客网
思路:递归从左右子树依次找,找到就返回,找不到就返回根节点。
Java版:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
// write code here
return find(root, o1, o2).val ;
}
public TreeNode find(TreeNode root, int o1, int o2) {
if (root == null || root.val == o1 || root.val == o2) {
return root ;
}
TreeNode left = find(root.left, o1, o2) ;
TreeNode right = find(root.right, o1, o2) ;
if(left == null){
return right ;
}
if(right == null){
return left ;
}
return root ;
}
}
17、重建二叉树
题目链接:重建二叉树_牛客题霸_牛客网
思路:找到根节点,递归构建左右子树即可。
Java版:
import java.util.*;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
//二叉树的先序用来找到根节点,二叉树的中序遍历用来找到左右子树
if(pre.length == 0 || vin.length == 0){
return null ;
}
TreeNode root = new TreeNode(pre[0]) ;
for(int i=0; i<vin.length;i++){
if(pre[0] == vin[i]){
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(vin, 0, i)) ;
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length), Arrays.copyOfRange(vin,i+1,vin.length)) ;
break ;
}
}
return root ;
}
}
千里之行,始于足下,秋招之行,行则将至。