关于树
import java.util.*; public class Solution { String Serialize(TreeNode root) { Queue<TreeNode> queue=new LinkedList<>(); StringBuilder result=new StringBuilder(); queue.offer(root); while(!queue.isEmpty()) { /* 这种层序不是完全二叉树,只是将每一层的结点串联起来,不包含空结点 int size=queue.size(); for(int i=0;i<size;i++) { TreeNode node=queue.poll(); result.append(node.val+","); if(node.left!=null) queue.offer(node.left); if(node.right!=null) queue.offer(node.right); } */ //可以在队列中添加null的空结点 TreeNode node=queue.poll(); if(node!=null) { queue.offer(node.left); queue.offer(node.right); result.append(node.val+","); } else { result.append("#"+","); } } if(result.length()!=0) result.deleteCharAt(result.length()-1); return result.toString(); } TreeNode Deserialize(String str) { TreeNode head =null; if(str==null || str.length()==0) return head; String[] nodes=str.split(","); TreeNode[] treenodes=new TreeNode[nodes.length]; for(int i=0;i<nodes.length;i++) { if(!nodes[i].equals("#")) { treenodes[i]=new TreeNode(Integer.valueOf(nodes[i])); } } for(int i=0,j=1;j<treenodes.length;i++) { if(treenodes[i]!=null) { treenodes[i].left=treenodes[j++]; treenodes[i].right=treenodes[j++]; } } return treenodes[0]; } }
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ //根据前序遍历规则完成序列化与反序列化 //算法思想:根据前序遍历规则完成序列化与反序列化。所谓序列化指的是遍历二叉树为字 //符串;所谓反序列化指的是依据字符串重新构造成二叉树。 依据前序遍历序列来序列化 //二叉树,因为前序遍历序列是从根结点开始的。当在遍历二叉树时碰到Null指针时,这些 //Null指针被序列化为一个特殊的字符“#”。 另外,结点之间的数值用逗号隔开。 public class Solution { String Serialize(TreeNode root) { StringBuilder res=new StringBuilder(); if(root==null) { res.append("#,"); return res.toString(); } res.append(root.val+","); res.append(Serialize(root.left)); res.append(Serialize(root.right)); return res.toString(); } int index=-1; TreeNode Deserialize(String str) { index++; String[] strr=str.split(","); TreeNode node=null; if(!strr[index].equals("#")) { node=new TreeNode(Integer.valueOf(strr[index])); node.left=Deserialize(str); node.right=Deserialize(str); } return node; } }

/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { if(root1==null || root2==null) { return false; } if(root1.val==root2.val) { if(judge(root1,root2)) return true; } return HasSubtree(root1.left,root2) || HasSubtree(root1.right, root2); } public boolean judge(TreeNode root, TreeNode subtree) { if(subtree==null) return true; if(root==null) return false; if(root.val==subtree.val) return judge(root.left, subtree.left)&&judge(root.right,subtree.right); return false; } }

/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public int TreeDepth(TreeNode root) { if(root==null) return 0; //int left=TreeDepth(root.left); //int right=TreeDepth(root.right); return Math.max(TreeDepth(root.left)+1,TreeDepth(root.right)+1); } }

/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public void Mirror(TreeNode root) { if(root==null) return; else{ TreeNode node =root.left; root.left=root.right; root.right=node; } Mirror(root.left); Mirror(root.right); } }

class Solution { public boolean isBalanced(TreeNode root) { if (root == null) return true; return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right); } private int depth(TreeNode root) { if (root == null) return 0; return Math.max(depth(root.left), depth(root.right)) + 1; } }
二叉搜索树的中序遍历就是递增序列
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*; class Solution { ArrayList<Integer> list=new ArrayList<>(); public int kthLargest(TreeNode root, int k) { if(root==null) return 0; inOrder(root); return(list.get(list.size()-k)); } public void inOrder(TreeNode root){ if(root!=null){ inOrder(root.left); list.add(root.val); inOrder(root.right); } } }

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*; class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; int left=maxDepth(root.left); int right=maxDepth(root.right); return Math.max(left,right)+1; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧