剑指offer搜索与回溯算法题解

剑指32.从上到下打印二叉树

class Solution {

    public int[] levelOrder(TreeNode root) {

        if (root == null) return new int[0];

        ArrayList<Integer> ans = new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);

        while (!queue.isEmpty()) {

           TreeNode node = queue.poll();

           ans.add(node.val);

           if (node.left != null) queue.add(node.left);

           if (node.right != null) queue.add(node.right);

        }

        int[] res = new int[ans.size()];

         for (int i = 0; i < ans.size(); i++) {

            res[i] = ans.get(i);

         }

         return res;

    }

}

BFS搜索的模板解法。使用队列。

剑指32-2.从上到下打印二叉树2

class Solution {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {

         dfs(0,root);

         return res;

    }

  

    void dfs(int depth, TreeNode root) {

       if (root == null) return;

       if (res.size() == depth) {

           res.add(new ArrayList<>());

       }

       res.get(depth).add(root.val);

       dfs(depth + 1, root.left);

       dfs(depth + 1, root.right);

    }

}

dfs递归算法的模板解法。

剑指32-3 从上到下打印二叉树3

class Solution {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {

          dfs(0, root);

          return res;

    }

   

    void dfs(int depth, TreeNode root) {

        if (root == null) return;

        if (res.size() == depth) {

            res.add(new ArrayList<>());

        }

        if (depth % 2 == 0){

            res.get(depth).add(root.val);

        } else {

            res.get(depth).add(0, root.val);

        }

        dfs(depth + 1, root.left);

        dfs(depth + 1, root.right);

    }

}

同样是dfs的模板,唯一区别是第二层要倒序添加倒序添加为add(0, root.val)。

剑指26.树的子结构

class Solution {

    public boolean isSubStructure(TreeNode A, TreeNode B) {

        return (A != null && B != null) && (dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));

    }

    boolean dfs(TreeNode A, TreeNode B) {

        if(B == null) return true;

        if(A == null || A.val != B.val) return false;

        return dfs(A.left, B.left) && dfs(A.right, B.right);

    }

}

dfs搜索,是子结构有三种情况1.相等 2.B是A左子树的 3.B是A的右子树。

剑指27.二叉树的镜像

class Solution {

    public TreeNode mirrorTree(TreeNode root) {

      if (root == null) return null;

      TreeNode tmp = root.left;

      root.left = mirrorTree(root.right);

      root.right = mirrorTree(tmp);

      return root;

    }

}

二叉树左右子树交换,采用递归方法交换方法的固定思路,建立中间变量。之后递归调用mirrorTree函数交换左右子树。

剑指28.对称的二叉树

class Solution {

    public boolean isSymmetric(TreeNode root) {

        return root == null ? true : dfs(root.left, root.right);

    }

    boolean dfs(TreeNode left, TreeNode right) {

        if (left == null && right == null) return true;

        if (left == null || right == null || left.val != right.val) return false;

        return dfs(left.right, right.left) && dfs(left.left, right.right);

    }

}

对称二叉树的判断,采用了递归方法,递归调用dfs函数判断left.right和right.right是否相等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值