首页 > 编程语言 >整理一些关于树的力扣热门算法操作

整理一些关于树的力扣热门算法操作

时间:2022-10-27 11:36:33浏览次数:45  
标签:right return 热门 力扣 算法 TreeNode null root left


整理一些关于树的力扣热门算法操作_算法

一、LC94、144、145中序,前序,后续遍历

List<Integer> front = new ArrayList<>();
List<Integer> mid = new ArrayList<>();
List<Integer> back = new ArrayList<>();

//前序遍历
public void Preorder(TreeNode root){
if(root == null)
return;
front.add(root.val);
Preorder(root.left);
Preorder(root.right);
}

//中序遍历
public void Inorder(TreeNode root){
if(root == null)
return;
Inorder(root.left);
mid.add(root.val);
Inorder(root.right);
}

//后序遍历
public void Postorder(TreeNode root){
if(root == null)
return;
Postorder(root.left);
Postorder(root.right);
back.add(root.val);
}

二、LC104、111 二叉树最大 最小深度

// 最大深度
public int maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
}

// 最小深度
public int minDepth(TreeNode root) {
if (root == null){
return 0;
}else if (root.left == null || root.right == null){
return 1;
}else{
return Math.min(minDepth(root.left),minDepth(root.right)) + 1;
}
}

三、LC100 是否同一棵树

public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null){
return true;
}
if(p!=null && q!=null && p.val==q.val){
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
return false;
}

四、LC101 是否对称二叉树

public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return dfs(root.left,root.right);
}

public boolean dfs(TreeNode root1,TreeNode root2){
if(root1==null&&root2==null) return true;
if(root1==null || root2==null || root1.val != root2.val) return false;
return dfs(root1.left,root2.right)&&dfs(root1.right,root2.left);
}

五、LC110 判断平衡二叉树

public boolean isBalanced(TreeNode root) {
if(root==null) return true;
if(Math.abs(maxDepth(root.left) - maxDepth(root.right)) <=1 ){
return isBalanced(root.left)&&isBalanced(root.right);
}
return false;
}

private int maxDepth(TreeNode root){
if(root==null) return 0;
return Math.max(getHigh(root.left),getHigh(root.right))+1;
}

六、LC112 二叉树路径 目标值总和

class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null) {
return sum == root.val;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}

七、LC226 翻转二叉树

public static TreeNode invertTree(TreeNode root) {
if (root == null || (root.right == null && root.left == null)){
return root;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}

七、判断是否为子树【子树,要么等于,要么等于左/右子树。】

public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) return false;
//子树,要么等于,要么等于左/右子树。
return subFrom(root,subRoot) || isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
}


public boolean subFrom(TreeNode root, TreeNode subRoot){
if (root == null && subRoot == null) return true;
if (root == null || subRoot == null) return false;
if (root.val != subRoot.val) return false;
return subFrom(root.left, subRoot.left) && subFrom(root.right, subRoot.right);
}

八、LC589、590 N叉树的前序 后续遍历

// 前序遍历
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> preorder(Node root) {
//递归版
if(root == null)
return res;
res.add(root.val);
for(Node child : root.children)
{
preorder(child);
}
return res;
}
}

// 后序遍历
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> postorder(Node root) {
//递归版
if(root == null)
return res;

for(Node child : root.children)
{
postorder(child);
}
res.add(root.val);
return res;
}
}

九、LC559 N叉树的最大深度

public int maxDepth(Node root) {
if(root == null) return 0;
int max = 0;
for(Node node : root.children){
max = Math.max(maxDepth(node),max);
}
return max + 1;
}

十、Offer27 二叉树的镜像

public TreeNode mirrorTree(TreeNode root) {

if (root == null)return null;

TreeNode tmpNode = root.left;
root.left = mirrorTree(root.right);
root.right = mirrorTree(tmpNode);

return root;
}

十一、NC 是否为二叉搜索树和完全二叉树

public boolean[] judgeIt (TreeNode root) {
// write code here
boolean bst = isBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
boolean ct = isCompeteTree(root);
return new boolean[]{bst, ct};
}

public boolean isBST(TreeNode root, long lower, long upper) {
if (null == root) return true;
if (root.val <= lower || root.val >= upper) {
return false;
}
return isBST(root.left, lower, root.val) && isBST(root.right, root.val, upper);
}

public boolean isCompeteTree(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

while (queue.peek() != null) {
TreeNode last = queue.poll();
queue.offer(last.left);
queue.offer(last.right);
}

while (!queue.isEmpty() && queue.peek() == null) {
queue.poll();
}

return queue.isEmpty();
}

十二、二叉树的最大路径之和

public class MaxPathSumTree {

int max =Integer.MIN_VALUE;

public int maxPathSum (TreeNode root) {
if (root == null) return 0;
help(root);
return max;
}

public int help(TreeNode root) {
if(root == null) {
return 0;
}
//如果子节点为负数节点 让他的计算值为0 则一个节点的最大值为 root+left + right
int left = Math.max(help(root.left), 0);
int right = Math.max(help(root.right), 0);
max = Math.max(max, root.val + left + right);
return Math.max(root.val + left, root.val + right);
}
}

十三、LC404 求二叉树左叶子之和

public static int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
if(root == null) return 0;

if(root.left != null ){
if(root.left.left == null && root.left.right == null){
sum += root.left.val;
}
}
sumOfLeftLeaves(root.left);
sumOfLeftLeaves(root.right);
return sum;
}

十四、LC543 二叉树的最大直径【最大路径长度】

class Solution {
private int max = 0;

public int diameterOfBinaryTree(TreeNode root) {
dfs(root);
return max;
}

private int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = dfs(root.left);
int rightHeight = dfs(root.right);
max = Math.max(leftHeight + rightHeight, max);
return Math.max(leftHeight, rightHeight) + 1;
}
}

十五、LC102 二叉树层次优先遍历

// 深度优先遍历:
void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
dfs(root.right);
}

// 广度优先遍历
void bfs(TreeNode root) {
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // Java 的 pop 写作 poll()
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}

这个题目层次遍历,应该选用BFS

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (root == null) {
return ret;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<Integer>();
int currentLevelSize = queue.size();
for (int i = 1; i <= currentLevelSize; ++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);
}
}
ret.add(level);
}

return ret;
}
}


标签:right,return,热门,力扣,算法,TreeNode,null,root,left
From: https://blog.51cto.com/u_15848894/5800562

相关文章

  • 整理牛客网---阿里校招笔试后端Java版,dfs和算法题。
    一、2021(校招)阿里巴巴7.22笔试(Java版)1.1题目1给定一个n,求[1,n]这n个数字的排列组合有多少个。条件:相邻的两个数字的绝对值不能等于1.例如:4[2,4,1,3][3,1,4......
  • 【Alink-KMeans】基于Alink算法平台的聚类【Java实现】
    一、介绍Alink是基于Flink的通用算法平台。1.1数据聚类介绍1.可以定义为5组数据类型的特征字段名称:sepal_lengthdouble,sepal_widthdouble,petal_lengthdouble,peta......
  • 代码随想录 KMP算法,实现 strStr()(LeetCode 028)和重复的子字符串(LeetCode 459)
    KMP算法前缀表:记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串。后缀是指不包含第一个字符的所......
  • 力扣455(java&python)-分发饼干(简单)
    题目:假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;......
  • 老生常谈React的diff算法原理-面试版
    第一次发文章notonly(虽然)版式可能有点烂butalso(但是)最后赋有手稿研究finally看完他你有收获diff算法:对于update的组件,他会将当前组件与该组件在上次更新是对应的......
  • 【数据结构与算法1】什么是算法和数据结构
    一、什么是算法和数据结构1、算法 合理的算法可以使我们合理运用空间,提高计算的效率,达到事半功倍的效果,算法的学习对于每个程序员来说是必不可少的。衡量算法好坏的标......
  • 每日算法4:去除数组中重复的元素,判断回文数/字符串
    去除数组中重复的元素题目描述:去除数组中重复的元素解题思路:因为对象中的键是唯一的,利用对象筛选数组中重复的部分,创建新数组存放不重复的元素因为对象中的键是唯一......
  • java排序算法(Java排序算法图解)
    如何理解排序算法的C++算法?排序算法C++算法编辑C++自带的algorithm库函数中提供了排序算法如何理解排序算法的C++算法?排序算法C++算法编辑C++自带的algorithm库函数中提供了......
  • 力扣182(java&python)-数组元素积的符号(简单)
    题目:已知函数 signFunc(x)将会根据x的正负返回特定值:如果x是正数,返回1。如果x是负数,返回-1。如果x是等于0,返回0。给你一个整数数组nums。令product......
  • 算法汇总
    一、枚举算法思想(暴力算法)将问题的所有可能答案一一列举,根据判断条件判断此答案是否合适,一般用循环实现。经典运用:百钱买百鸡、填写运算符二、递推算法思想1.顺推法:从已......