隐藏页面特效

随笔分类 -  关于树的算法题

leetcode以及牛客网等算法题
摘要://思路:回溯法(先序遍历+路径记录) //关于回溯法的模板网上很多,大家可以去查一下 class Solution { LinkedList<List<Integer>> res = new LinkedList<>(); LinkedList<Integer> path = new Linked 阅读全文 »
posted @ 2021-03-10 17:58 ForMei 阅读(53) 评论(0) 推荐(0) 编辑
摘要:思路:1.首先理解平衡二叉树的定义:任意节点的左右子树的高度差不超过1。 2. class Solution{ public boolean isPin(TreeNode root){ return recur(root)!=-1; } public int recuer(TreeNode root 阅读全文 »
posted @ 2021-03-07 15:30 ForMei 阅读(82) 评论(0) 推荐(0) 编辑
摘要:3.输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字 思路: 1.根据前序遍历可以找到根节点 2.迭代中序遍历结果,找到根节点的索引,左边是左子树,右边是右节点 3.递归处理左右子树,注意Arrays的函数区间左闭右开 public cl 阅读全文 »
posted @ 2021-03-06 13:02 ForMei 阅读(79) 评论(0) 推荐(0) 编辑
摘要:思路:注意recur方法中if(root2==null) return true;的理解,第一次调用recur的时候不会触发该条件的判断。 public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) 阅读全文 »
posted @ 2021-03-05 22:44 ForMei 阅读(48) 评论(0) 推荐(0) 编辑
摘要:思路:就是思考对称的时候的情况,然后用递归处理。 public class Solution { public boolean isSym(TreeNode root){ if(root==null) return true; return recur(root.left,root.right); 阅读全文 »
posted @ 2021-03-05 21:27 ForMei 阅读(76) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示