二叉树
前,中,后序遍历
前序(先序):根 左 右
中序:左 根 右
后续:左 右 根
public static void preOrder(Node root){
if(root==null){
return;
}
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
public static void inOrder(Node root){
if(root==null){
return;
}
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
public static void postOrder(Node root){
if(root==null){
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
层序遍历
思想:
public void levelOrder(Node root){
Queue<Node> queue=new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
Node cur=queue.poll();
System.out.print(cur.val+" ");
if(cur.left!=null){
queue.offer(cur.left);
}
if(cur.right!=null){
queue.offer(cur.right);
}
}
}
求节点个数
public static int size(Node root){
if(root==null){
return 0;
}
//结点个数等于根节点+左子树的节点个数+右子树的节点个数
return 1+size(root.left)+size(root.right);
}
求叶子结点个数
//遍历树,将问题拆分成求root.left的叶子节点个数+root.right的叶子节点个数
public static int leftSize(Node root){
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return 1;
}
return leftSize(root.left)+leftSize(root.right);
}
求第K层的节点个数
//如果K<1,只能是空树,直接返回0
//如果k==1;说明只有一个根节点,返回1
//第k层的节点个数=左子树的k-1层节点个数+右子树的(k-1)节点个数
public static int kLevelSize(Node root,int k){
if(k<1||root==null){
return 0;
}
if(k==1){
return 1;
}
return kLevelSize(root.left,k-1)+kLevelSize(root.right,k-1);
}
查找指定元素
//如果找到了返回引用,如果不存在返回null
Node find(Node root,int val){
if(root==null){
return null;
}
if(root.val==val){
return root;
}
Node result=find(root.left,val);
if(result!=null){
return result;
}
return find(root.right,val);
}
构建二叉树
class Node{
public char val;
public Node left;
public Node right;
public Node(char val) {
this.val = val;
}
}
public class TestTree {
public static Node build(){
Node a=new Node('A');
Node b=new Node('B');
Node c=new Node('C');
Node d=new Node('D');
Node e=new Node('E');
Node f=new Node('F');
Node g=new Node('G');
Node h=new Node('H');
a.left=b;
a.right=c;
b.left=d;
b.right=e;
e.left=g;
g.right=h;
c.right=f;
return a;
}