二叉树的最小深度:
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;
}
}