树的基础知识1-树的遍历+

树节点的定义
 Definition for a binary tree node.
 public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
 }
1.树的遍历(递归法)
//前序遍历
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preorder(root,res);
        return res;
    }
    public void preorder(TreeNode cur,List<Integer> res){
        if(cur == null) return;

        res.add(cur.val);
        preorder(cur.left,res);
        preorder(cur.right,res);
    }
}
//中序遍历
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inorder(root,res);
        return res;
    }
    public void inorder(TreeNode cur,List<Integer> res){
        if(cur == null) return;

        inorder(cur.left,res);//左
        res.add(cur.val);//中
        inorder(cur.right,res);//右
    }
}
//后序遍历
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        postorder(root,res);
        return res;
    }
    public void postorder(TreeNode cur,List<Integer> res){
        if(cur == null) return;

        postorder(cur.left,res);//左
        postorder(cur.right,res);//右
        res.add(cur.val);//中
    }
}
2.树的遍历(迭代法)

前序遍历是一路向左,边 处理边入栈
中序遍历是先一路向左,只入栈不处理
后序遍历,按照中左右的顺序边处理边入栈来做的(这是前序遍历的思想),但是利用头插法,即实现倒序,即按照前序遍历倒序将得到后序遍历。

//前序遍历
class Solution{
    public List<Integer> preorderTraversal(TreeNode root){
        Stack<TreeNode> stack = new Stack<TreeNode>();
        List<Integer> res = new ArrayList<>();//用于返回
        TreeNode node = root;
        while(!stack.empty() || node!=null){
            while(node != null){//先一路向左处理并入栈
                res.add(node.val);
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            node = node.right;
        }
        return res;
    }
}
//中序遍历
class Solution{
    public List<Integer> inorderTraversal(TreeNode root){
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;

        while(!stack.empty() || node!=null){
            while(node != null){//先一路向左,只入栈不处理
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            res.add(node.val);
            node = node.right;
        }
        return res;
    }
}
//后序遍历
class Solution{//后序遍历只需要在前序遍历的基础上做反转即可
    public List<Integer> postorderTraversal(TreeNode root){
        LinkedList<Integer> res = new LinkedList<>();//因为addFirst()法是LinkedList中的新生方法,其父类List中不存在,所以不能讲res定义成List类型,而是LinkedList类型
        Stack<TreeNode> st = new Stack<>();
        TreeNode node = root;
        st.push(node);
        while(!st.empty()){
            node = st.pop();
            if(node != null){
                res.addFirst(node.val);//头插法(头插法就可起到逆序的作用)
            }
            else continue;
            st.push(node.left);
            st.push(node.right);
        }//入栈顺序:中左右,出栈处理顺序:中右左,将其反转就是:左右中(头插法就相当于反转)
        return res;
    }
}

=====================================

树的最大深度(也称树的深度)
  1. 二叉树的最大深度:
    递归法:用后序遍历(左右中)
    先求左子树深度,再求右子树深度,树的最大深度等于左右子树深度的最大值+1(根节点或当前中间节点)
    迭代法:层序遍历,最大深度也就是层数
 //二叉树的最大深度(递归法)
class Solution {
    public int maxDepth(TreeNode root) {
        return getDepth(root);
    }
    int getDepth(TreeNode node){
        if(node == null) return 0;
        int leftDepth = getDepth(node.left);//当前节点左子树的深度
        int rightDepth = getDepth(node.right);//当前节点右子树的深度
        int depth = (leftDepth>rightDepth?leftDepth:rightDepth) + 1;//当前节点的最大深度等于左右子树最大深度+1(当前节点)
        return depth;
    }
}

//二叉树的最大深度(递归法精简)
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}

 //二叉树的最大深度(迭代法)
class Solution{
    public int maxDepth(TreeNode root){
        Queue<TreeNode>que = new LinkedList<TreeNode>();
        que.offer(root);
        int depth = 0;
        while(que.peek() != null){
            depth++;
            int currentLevelSzie = que.size();
            for(int i = 1;i <= currentLevelSzie;i++){
                TreeNode node = que.poll();
                if(node.left != null) que.offer(node.left);
                if(node.right != null) que.offer(node.right);
            }        
        }
        return depth;
    }
}
  1. N叉树的最大深度 node.children
    递归法
    迭代法
//递归法
class Solution {
    public int maxDepth(Node root) {
        return getDepth(root);
    }
    //int depth = 0; 不能放在函数外面,要不然这句初始化不执行
    int getDepth(Node node){//N叉树的节点不是TreeNode,而是Node,根据节点定义得
        int depth = 0; //这个初始化是在getDepth函数里用的,所以定义在函数里边
        if(node == null) return 0;
        for(Node child : node.children){//找出所有孩子节点树种深度最大的
            depth = Math.max(depth, getDepth(child));
        }
        return depth + 1;
    }
}

//迭代法
class Solution{
    public int maxDepth(Node root){
        Queue<Node>que = new LinkedList<>();
        que.offer(root);
        int depth = 0;
        while(que.peek() != null){
            depth++;
            int currentLevelSize = que.size();
            for(int i = 1;i <= currentLevelSize;i++){
                Node node = que.poll();
                for(Node child : node.children){
                    que.offer(child);
                }
            }
        }
        return depth;
    }
}
树的最小深度(与最大深度的区别)
  1. 二叉树的最小深度
    最大深度:从根节点到最远叶子结点
    最小深度:从根节点到最近叶子结点(叶子结点指左右孩子都为空的节点)
    特殊注意:根节点的左子树或者右子树为空时,最小深度并不为1,而是取决于非空子树的最小深度。(因为是根节点到叶子结点的最小距离)

在这里插入图片描述

class Solution {
    public int minDepth(TreeNode root) {
        //最小深度:根节点到最近叶子结点的距离(叶子结点指左右孩子都为空)
        //基本操作跟最大深度差不多,但是有特殊情况(根节点的某个子树为空)需要单独处理一下
        if(root == null) return 0;
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);

        //特殊:若根节点的左子树为空,则最小深度得看右子树
        if(root.left == null && root.right != null){
            return 1 + rightDepth;
        }
        //若根节点的右子树为空,则最小深度得看左子树
        if(root.left != null && root.right == null){
            return 1 + leftDepth;
        }

        return 1 + Math.min(leftDepth,rightDepth);//正常情况下(左右子树都不为空)
    }
}
110. 平衡二叉树
  1. 平衡二叉树的定义:一个二叉树中每个节点的左右子树高度差绝对值不大于1 ,就是平衡二叉树。
  2. 节点的高度:指该节点到叶子节点的最远路径,也就是指该节点到最远的叶子节点的距离。
  3. 节点的深度:指根节点到该节点的距离(leetcode中高度和深度都是按照路径中的节点数量计算的)
  4. 节点的高度公式:
    在这里插入图片描述
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(height(root) == -1) return false;
        else return true;
    }
    
    int height(TreeNode node){
        if(node == null) return 0;
        int leftHeight = height(node.left);
        int rightHeight = height(node.right);
        //如果某个节点的左子树或右子树不是平衡树,则该节点也不是平衡树,如果该节点的左右子树是平衡树,才看其高度差(return -1只是结束本次函数,递归并不会结束)
        if(leftHeight==-1 || rightHeight==-1 || Math.abs(leftHeight-rightHeight)>1)
            return -1;
        //如果某个节点的左右子树高度差绝对值<=1,则该节点是平衡二叉树,可以继续判断,所以需要返回该节点的高度(因为还要跟别的节点的高度比较)
        else{
            int heig = Math.max(leftHeight, rightHeight) + 1;
            return heig;
        }
     }
}

注意:采用自底向上的方法,效率高
判断某个节点平衡的条件:左子树平衡且右子树平衡,再判断其左右子树高度差<=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值