
树
蛋卷在月球烤面包
这个作者很懒,什么都没留下…
展开
-
力扣572 另一棵树的子树
思路采用先序遍历的思想遍历二叉树。原创 2022-08-01 11:51:03 · 138 阅读 · 0 评论 -
力扣450 删除二叉搜索树中的节点
思路:先根据二叉搜索树的性质查找目标结点,找到之后判断这个节点左右子节点是否为空,都不为空:遍历右子树的左节点,找到最左的叶子结点作为新的根节点。 public TreeNode deleteNode(TreeNode root, int key){ if(root==null) return null; if(key>root.val){ root.right=deleteNode(root.right,key); .原创 2022-05-10 20:09:16 · 148 阅读 · 0 评论 -
力扣662 二叉树最大宽度
思路:一开始考虑用BFS,但是题目要求null也算在宽度之内,但如果node.left为空是无法入队的,所以就改变思路,将每个结点都对应一个坐标,利用规律根结点下标i,左孩子为2*i,右孩子为2*i+1。public int widthOfBinaryTree(TreeNode root) { int res=0; Queue<TreeNode> queue=new LinkedList<>(); queue.add(root原创 2022-03-15 20:08:53 · 524 阅读 · 0 评论 -
力扣958 二叉树的完全性检验
思路:层次遍历整个二叉树,完全二叉树应该是连续的结点,而如果前一个结点为空,也就是不连续,就说明不是完全二叉树,设置结点pre记录上一个结点。public boolean isCompleteTree(TreeNode root){ Queue<TreeNode> queue=new LinkedList<>(); queue.add(root); TreeNode pre=root; while(!queue.i原创 2022-03-15 18:00:13 · 753 阅读 · 0 评论 -
力扣617 合并二叉树
方法一:递归public TreeNode mergeTrees(TreeNode t1,TreeNode t2){ if(t1==null||t2==null){ return t1==null?t2:t1; } t1.val+=t2.val; t1.left=mergeTrees(t1.left,t2.left); t1.right=mergeTrees(t1.right,t2.right);原创 2022-01-24 17:08:18 · 158 阅读 · 0 评论 -
力扣543二叉树的直径
思路:二叉树的直径=根结点的左右子树高度相加 int max=0; public int diameterOfBinaryTree(TreeNode root) { dfs(root); return max; } public int dfs(TreeNode root){ if(root==null) return 0; int left=dfs(root.left); int righ.原创 2022-01-24 16:50:38 · 229 阅读 · 0 评论 -
力扣538 把二叉搜索树转换为累加树
int sum=0; public TreeNode convertBST(TreeNode root){ if(root==null) return null; //倒转的中序遍历 convertBST(root.right); sum+=root.val; root.val=sum; convertBST(root.left); return root; }...原创 2022-01-24 15:59:23 · 135 阅读 · 0 评论 -
打家劫舍问题合集
力扣198public int rob(int[] nums){ int len=nums.length; if(len==0) return 0; int[] dp=new int[len+1]; dp[0]=0; dp[1]=nums[0]; for(int i=2;i<=len;i++){ dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i-1])原创 2022-01-24 15:50:17 · 147 阅读 · 0 评论 -
力扣226 翻转二叉树
方法一:递归 采用先序遍历public TreeNode invertTree(TreeNode root){ if(root==null) return null; TreeNode temp=root.left; root.left=root.right; root.right=temp; invertTree(root.left); invertTree(root.right); re原创 2022-01-24 14:08:31 · 404 阅读 · 0 评论 -
力扣124 二叉树中的最大路径和
思路:二叉树中的最大路径和,采用后序遍历,自底向上。 int max=Integer.MIN_VALUE; public int maxPathSum(TreeNode root){ if(root==null) return 0; dfs(root); return max; } public int dfs(TreeNode root){ if(root==null) return 0;原创 2022-01-24 11:44:22 · 137 阅读 · 0 评论 -
力扣114 二叉树展开为链表
方法一:public void flatten(TreeNode root) { if(root == null){ return ; } //将根节点的左子树变成链表 flatten(root.left); //将根节点的右子树变成链表 flatten(root.right); TreeNode temp = root.right; //把树的右原创 2022-01-24 10:09:44 · 238 阅读 · 0 评论 -
力扣98 验证二叉搜索树
思路:验证二叉搜索树就需要遍历,遍历二叉搜索树可以采用中序遍历,这样遍历的结果是升序的,为了验证是否是升序,设置结点preNode来保存上一个访问过的结点。TreeNode preNode=null; public boolean isValidBST(TreeNode root){ if(root==null) return true; if(!isValidBST(root.left)) return false; if(preNode!=.原创 2022-01-23 18:11:52 · 198 阅读 · 0 评论 -
力扣96 不同的二叉搜索树
假设n个节点存在令G(n)的从1到n可以形成二叉排序树个数令f(i)为以i为根的二叉搜索树的个数即有:G(n) = f(1) + f(2) + f(3) + f(4) + ... + f(n)n为根节点,当i为根节点时,其左子树节点个数为[1,2,3,...,i-1],右子树节点个数为[i+1,i+2,...n],所以当i为根节点时,其左子树节点个数为i-1个,右子树节点为n-i,即f(i) = G(i-1)*G(n-i),上面两式可得:G(n) = G(0)*G(n-1)+G(1)*(原创 2022-01-23 17:20:49 · 93 阅读 · 0 评论 -
力扣94 二叉树的中序遍历
方法一:List<Integer> res=new LinkedList<>(); public List<Integer> inorderTraversal(TreeNode root) { dfs(root); return res; } public void dfs(TreeNode root){ if(root==null) return; dfs(root.left)原创 2022-01-23 16:54:20 · 84 阅读 · 0 评论 -
二叉树的最近公共祖先合集
剑指offer86:在二叉树中查找public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){ if(root==null||root==p||root==q) return root; TreeNode left=lowestCommonAncestor(root.left,p,q); TreeNode right=lowestCommonAncestor(ro原创 2022-01-23 15:55:28 · 397 阅读 · 0 评论 -
剑指offer37 序列化二叉树
public String Serialize(TreeNode root){ if(root==null) return "{}"; StringBuilder string=new StringBuilder("{"); Queue<TreeNode> queue=new LinkedList<TreeNode>(){{add(root);}}; while(!queue.isEmpty()){ .原创 2022-01-23 15:01:11 · 104 阅读 · 0 评论 -
剑指offer8 二叉树的下一个结点
思路:因为是二叉搜索树,所以每个子树的根结点大于左子树,小于右子树。因此二叉树的下一个结点分两种情况讨论,有右结点如6和没有右结点。没有右结点又分为当前结点是左孩子如5还是右孩子如7。public TreeLinkNode GetNext(TreeLinkNode pNode) { if(pNode==null) return null; if(pNode.right!=null){ pNode=pNode.right;原创 2022-01-22 19:15:01 · 72 阅读 · 0 评论 -
剑指offer55 平衡二叉树
方法一:自底向上public boolean isBalanced(TreeNode root){ return Solve(root)!=-1; } public int Solve(TreeNode root){ if(root==null) return 0; int left=Solve(root.left); if(left==-1) return -1;//剪枝 int right=S原创 2022-01-22 17:51:45 · 67 阅读 · 0 评论 -
剑指offer36 二叉搜索树与双向链表
TreeNode pre,head; public TreeNode Convert(TreeNode pRootOfTree) { dfs(pRootOfTree); return head; } public void dfs(TreeNode cur){ if(cur==null) return; dfs(cur.left); if(pre!=null) pre.right=cur; ...原创 2022-01-22 17:16:08 · 67 阅读 · 0 评论 -
二叉树中和为某一值的路径合集
剑指offer82:public boolean hasPathSum (TreeNode root, int sum) { if(root==null) return false; sum-=root.val; if(sum==0&&root.left==null&&root.right==null) return true; return hasPathSum(root.left,sum)||hasPa原创 2022-01-22 16:07:03 · 206 阅读 · 0 评论 -
剑指offer33 二叉搜索树的后序遍历序列
方法一:递归分治 public boolean verifyPostorder(int[] postorder){ return Solve(postorder,0,postorder.length-1); } public boolean Solve(int[] postorder,int i,int j){ if(i>=j) return true;//表示此时子树结点<=1 int m=i; w.原创 2022-01-22 12:33:19 · 243 阅读 · 0 评论 -
剑指offer27 二叉树的镜像
方法一:递归public TreeNode mirrorTree(TreeNode root){ if(root==null) return null; TreeNode temp=root.left; root.left=mirrorTree(root.right); root.right=mirrorTree(temp); return root; }方法二:层次遍历public TreeNode .原创 2022-01-21 23:35:31 · 99 阅读 · 0 评论 -
对称性递归合集
剑指offer26 树的子结构://先找到A中和B根结点一样的结点public boolean isSubStructure(TreeNode A, TreeNode B){ if(A==null||B==null) return false; return Solve(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B); }//找到后开始遍历判断是否一样 public boolean原创 2022-01-21 23:10:29 · 214 阅读 · 0 评论 -
剑指offer7 重建二叉树
思路:本题采用分治算法。根据先序遍历确定根结点,再根据根结点确定中序遍历中的左右子树。 int[] preorder; HashMap<Integer,Integer> dic=new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder){ this.preorder=preorder;//先序 for(int i=0;i<inor.原创 2022-01-21 22:48:38 · 69 阅读 · 0 评论 -
剑指offer54 二叉搜索树的第K个结点
思路:查找第K小的结点。二叉搜索树按照中序遍历就是从小到大的顺序排列,所以直接套用中序遍历的思想,递归即可。(力扣上是第K大的结点,同理,只要先遍历root.right后遍历root.left即可。 int res=-1;//需要考虑proot=null的情况,所以默认为-1 int count; public int KthNode(TreeNode proot, int k){ this.count=k; dfs(proot);原创 2022-01-21 21:38:14 · 200 阅读 · 0 评论 -
打印二叉树合集
力扣 面试题32 - I. 从上到下打印二叉树public int[] levelOrder(TreeNode root) { if(root==null) return new int[0]; Queue<TreeNode> queue=new LinkedList<>(); queue.add(root); ArrayList<TreeNode> temp=new ArrayList<>原创 2022-01-21 20:51:01 · 444 阅读 · 0 评论 -
求二叉树深度合集
剑指offer55 二叉树的深度递归:(DFS)public int maxDepth(TreeNode root) { if(root==null) return 0; return Math.max(maxDepth(root.left),maxDepth(root.right))+1; }层次遍历BTS:public int maxDepth(TreeNode root) { if(root==null) return 0;原创 2022-01-21 17:48:33 · 308 阅读 · 0 评论 -
树的遍历方式总结
二叉树的遍历:先序遍历、中序遍历、后序遍历、层序遍历。先序遍历:ABDFECGHI中序遍历:DBEFAGHCI后续遍历:DEFBHGICA层次遍历:ABCDFGIEH前中后序遍历的写法有两类:递归和非递归前序遍历public static void preOrderTraveral(TreeNode node){ if(node == null){ return; } System.out.print(原创 2022-01-21 16:06:54 · 734 阅读 · 0 评论