leetcode学习之剑指offer复习: 二叉树

二叉树的最小深度:
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

import java.util.LinkedList;
import java.util.Queue;
public class TreeMinDepth {
      //递归方法
//    public int minDepth(TreeNode root) {
//          if(root ==null)
//                return 0;
//          if(root.left==null && root.right==null){
//                return 1;
//          }
//          if(root.left==null)
//                return minDepth(root.right)+1;
//          if(root.right==null)
//                return minDepth(root.left)+1; 
//          return Math.min(minDepth(root.left), minDepth(root.right))+1;
//          
//    }
      //非递归方法
      public int minDepth(TreeNode root) {
            if(root ==null)
                  return 0;
            int depth=0;
            Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            depth++;
            int curSize= queue.size();
            for(int i=0;i<curSize;i++){
                TreeNode treeroot = queue.poll();
                //找到第一个左右节点都为空的节点即返回
                if(treeroot.left==null && treeroot.right==null)
                  return depth;
                if(treeroot.left!=null){

                    queue.offer(treeroot.left);
                }

                if(treeroot.right!=null){

                    queue.offer(treeroot.right);
                }

            }
        }
        return depth;

    }
}

剑指offer类似题目:
二叉树的深度:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

import java.util.Queue;
import java.util.LinkedList;

public class Solution {
    //递归的方法
    public int TreeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftLength = TreeDepth(root.left);
        int rightLength = TreeDepth(root.right);
        if (leftLength > rightLength) {
            return leftLength + 1;
        } else {
            return rightLength + 1;
        }
    }
    //非递归的方法  
    /*
    public int TreeDepth(TreeNode root) {
        int depth=0;
        if(root==null){
            return depth;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            depth++;
            int curSize= queue.size();
            for(int i=0;i<curSize;i++){
                TreeNode treeroot = queue.poll();
                if(treeroot.left!=null){

                    queue.offer(treeroot.left);
                }
                if(treeroot.right!=null){

                    queue.offer(treeroot.right);
                }
            }
        }
        return depth;

    }*/
}

平衡二叉树的判断:

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null)
            return true;
        if(IsBalanced_Solution(root.left)==false)
            return false;
        if(IsBalanced_Solution(root.right)==false)
            return false;
        int leftLength = treeDepth(root.left);
        int rightLength = treeDepth(root.right);
        if (Math.abs(leftLength-rightLength)>1) {
            return false;
        } else {
            return true;
        }
    }

    public int treeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftLength = treeDepth(root.left);
        int rightLength = treeDepth(root.right);
        if (leftLength > rightLength) {
            return leftLength + 1;
        } else {
            return rightLength + 1;
        }
    }
}

附加知识
二叉树的层序遍历

public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root==null){
            return list;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode treeroot = queue.poll();

            if(treeroot.left!=null){

                  queue.offer(treeroot.left);
            }
            if(treeroot.right!=null){

                  queue.offer(treeroot.right);
            }
            list.add(treeroot.val);

        }
        return list;
        }

分层打印二叉树

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<ArrayList<Integer>> arrayList= new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        if(pRoot==null){
            return arrayList;
        }
        deque.offer(pRoot);
        while(!deque.isEmpty()){
            int len= deque.size();
            for(int i=0;i<len;i++){
                TreeNode treeroot = deque.poll();
                list.add(treeroot.val);
                if(treeroot.left!=null){
                    deque.offer(treeroot.left);
                }
                if(treeroot.right!=null){
                    deque.offer(treeroot.right);
                }
            }
            arrayList.add(new ArrayList<Integer>(list) );
            list.clear();       
        }
        return arrayList;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值