1)java 求二叉树深度
算法很简单,就是比较左子节点深度 右节点深度
还有一个核心内容就是如果有值就+1,+1这个是整个算法的核心。
2)二叉树的层次遍历,采用queue遍历,先左后右遍历
3)二叉树的先序遍历方法,先push进去,然后再pop出来,结果正好相反。
package javatest.suanfa.Erchashu;
import java.util.*;
public class dept {
//二叉树的高度,后续遍历例子
int height(TreeNode T)
{
if (T==null) {
return 0;
}
int l=height(T.getLeftChild());
int r=height(T.getRightChild());
System.out.println("l==="+l+"---r=="+r+"------T==="+T.val);
if (l>r) {
return (l+1);
}
return (r+1);
}
/**
* 二叉树的queue层次优先遍历
* @param T
*/
public void cengci(TreeNode T)
{
if (T==null) {
return;
}
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(T);
while(!queue.isEmpty()){