一些值得注意的算法题——树

144. 二叉树的前序遍历

在这里插入图片描述

// 递归方法
// 先遍历当前节点,然后左子树,然后右子树
class Solution {
  List<Integer> ans = new ArrayList<>();

  public List<Integer> preorderTraversal(TreeNode root) {
    if (root == null) return ans;
    ans.add(root.val);
    preorderTraversal(root.left);
    preorderTraversal(root.right);
    return ans;
  }
}

// 迭代方法
// 因为栈的出入会逆序,所以先遍历当前节点,然后插入右子树,然后插入左子树
class Solution {
  List<Integer> ans = new ArrayList<>();
  Stack<TreeNode> s = new Stack<>();

  public List<Integer> preorderTraversal(TreeNode root) {
    if (root == null) return ans;

    s.push(root);
    while (!s.empty()) {
      root = s.pop();
      ans.add(root.val);
      if (null != root.right) s.push(root.right);
      if (null != root.left) s.push(root.left);
    }
    return ans;
  }
}

94. 二叉树的中序遍历

在这里插入图片描述

// 递归方法
// 先遍历左子树,然后当前节点,然后右子树
class Solution {
  List<Integer> ans = new ArrayList<>();

  public List<Integer> inorderTraversal(TreeNode root) {
    if (root == null) return ans;
    inorderTraversal(root.left);
    ans.add(root.val);
    inorderTraversal(root.right);
    return ans;
  }
}

// 迭代方法
// 先找到最左的节点,期间不断将经过的节点压入栈,出栈逆序,就是从左节点到根节点
// 然后遍历右子树,按照同样的方式找到最左的节点
class Solution {
  List<Integer> ans = new ArrayList<>();
  Stack<TreeNode> s = new Stack<>();

  public List<Integer> inorderTraversal(TreeNode root) {
    if (null == root) return ans;

    while (null != root || !s.empty()) {
      while (null != root) {
        s.push(root);
        root = root.left;
      }
      root = s.pop();
      ans.add(root.val);
      root = root.right;
    }
    return ans;
  }
}

145. 二叉树的后序遍历

在这里插入图片描述

// 递归方法
// 先遍历左子树,然后右子树,然后根节点
class Solution {
  List<Integer> ans = new ArrayList<>();

  public List<Integer> postorderTraversal(TreeNode root) {
    if (null == root) return ans;
    postorderTraversal(root.left);
    postorderTraversal(root.right);
    ans.add(root.val);
    return ans;
  }
}

// 迭代方法
// 前序遍历是【根节点,左子树,右子树】
// 后续遍历是【左子树,右子树,根节点】
// 又因为栈的出入顺序是逆序的
// 所以可以按照类似前序遍历【根节点,右子树,左子树】
// 把遍历结果压入另一个栈,最后逆序出栈
class Solution {
  List<Integer> ans = new ArrayList<>();
  Stack<TreeNode> s1 = new Stack<>();
  Stack<TreeNode> s2 = new Stack<>();

  public List<Integer> postorderTraversal(TreeNode root) {
    if (null == root) return ans;

    s1.push(root);
    while (!s1.empty()) {
      root = s1.pop();
      s2.push(root);
      if (null != root.left) s1.push(root.left);
      if (null != root.right) s1.push(root.right);
    }
    while (!s2.empty()) ans.add(s2.pop().val);
    return ans;
  }
}

105. 从前序与中序遍历序列构造二叉树

在这里插入图片描述

// 前序遍历:【根节点,【左子树】,【右子树】】
// 中序遍历:【【左子树】,根节点,【右子树】】
class Solution {
  HashMap<Integer, Integer> inorder_v2id = new HashMap<>();

  public TreeNode search(int[] preorder, int[] inorder, int pre_l, int pre_r, int in_l, int in_r) {
    if (pre_l > pre_r || in_l > in_r) return null;

    int pre_root_id = pre_l;
    int in_root_id = inorder_v2id.get(preorder[pre_root_id]);
    int left_size = in_root_id - in_l;

    TreeNode root = new TreeNode(preorder[pre_root_id]);
    root.left = search(preorder, inorder, pre_l + 1, pre_l + left_size, in_l, in_root_id - 1);
    root.right = search(preorder, inorder, pre_l + left_size + 1, pre_r, in_root_id + 1, in_r);
    return root;
  }

  public TreeNode buildTree(int[] preorder, int[] inorder) {
    int n = preorder.length;
    for (int i = 0; i < n; i++) {
      inorder_v2id.put(inorder[i], i);
    }
    return search(preorder, inorder, 0, n - 1, 0, n - 1);
  }
}

剑指offer 26. 树的子结构

在这里插入图片描述

// 依次对比当前节点和另一棵树根节点
// 对比左节点/右节点和另一棵树根节点
// 当相同时,递归对比左/右子树
class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }
    boolean recur(TreeNode A, TreeNode B) {
        if(B == null) return true;
        if(A == null || A.val != B.val) return false;
        return recur(A.left, B.left) && recur(A.right, B.right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值