高频算法题--二叉树

本文详细介绍了二叉树的各种操作,包括递归与迭代的遍历方式,二叉搜索树中第K小元素的查找,二叉树的重建方法,树的结构与性质,如对称性、平衡性,以及BFS的应用。同时涵盖了路径和节点链表的处理,如最大路径和、树的直径问题。
摘要由CSDN通过智能技术生成

二叉树

1、遍历

递归

ArrayList<Integer> preList = new ArrayList<>();
ArrayList<Integer> inList = new ArrayList<>();
ArrayList<Integer> postList = new ArrayList<>();
public int[][] threeOrders (TreeNode root) {
   
    // write code here
     preOrder(root);
    inOrder(root);
    postOrder(root);
    int[][] res = new int[3][preList.size()];
    res[0] = preList.stream().mapToInt(Integer::intValue).toArray();
    res[1] = inList.stream().mapToInt(Integer::intValue).toArray();
    res[2] = postList.stream().mapToInt(Integer::intValue).toArray();
    return res;
}

private void preOrder(TreeNode root){
   
    if(root == null) return;
    preList.add(root.val);
    preOrder(root.left);
    preOrder(root.right);
}

private void inOrder(TreeNode root){
   
    if(root == null) return ;
    inOrder(root.left);
    inList.add(root.val);
    inOrder(root.right);
}

private void postOrder(TreeNode root){
   
    if(root == null) return;
    postOrder(root.left);
    postOrder(root.right);
    postList.add(root.val);
}

迭代

前序

 private void preOrder1(TreeNode root){
   
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
   
            TreeNode node = stack.pop();
            preList.add(node.val);
            if(node.right != null) stack.push(node.right);
            if(node.left != null) stack.push(node.left);
        }
  }

中序

 private void inOrder1(TreeNode root){
   
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
   
            if(cur != null){
   
                stack.push(cur);
                cur = cur.left;//往左走,走到头
            }else{
   
                cur = stack.pop();//往中间走
                inList.add(cur.val);
                cur = cur.right;//往右走
            }
        }
    }

后序

 private void postOrder1(TreeNode root){
   
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
   
            TreeNode node = stack.pop();
            postList.add(node.val);
            if(node.left != null) stack.push(node.left);
            if(node.right != null) stack.push(node.right);
        }
        Collections.reverse(postList);
 }

二叉搜索树中第K小的元素

中序遍历递归

int count = 0,int res;
    public int kthSmallest(TreeNode root, int k) {
   
        inOrder(root,k);
        return res;
    }
    private void inOrder(TreeNode node,int k){
   
        if(node == null) return;
        inOrder(node.left,k);
        count++;
        if(count == k){
   
            res = node.val;
            return;
        }
        inOrder(node.right,k);
    }

中序遍历迭代

public int kthSmallest(TreeNode root, int k) {
   
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        int count = 0,res = 0;
        while(cur != null || !stack.isEmpty()){
   
            if(cur != null){
   
                stack.push(cur);
                cur = cur.left;
            }else{
   
                cur = stack.pop();
                count++;
                if(count == k){
   
                    res = cur.val;
                    break;
                }
                cur = cur.right;
            }
        }
        return res;
    }
   public int kthSmallest(TreeNode root, int k) {
   
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
   
            if(cur != null){
   
                stack.push(cur);
                cur = cur.left;
            }else{
   
                cur = stack.pop();
                if(--k == 0) break;
                cur = cur.right;
            }
        }
        return cur.val;

3、分治法

 public int kthSmallest(TreeNode root, int k) {
        int n = count(root.left);
        if(n + 1 < k){//表示需要结点在右子树中 n表示左子树的结点 1表示当前结点 
           return kthSmallest(root.right,k - n -1);
        }else if(n + 1 > k){
           return kthSmallest(root.left,k);
        }else{
            return root.val;
        }
    }
    private int count(TreeNode node){//计算节点数
        if(node == null) return 0;
        return 1 + count(node.left) &#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值