一.二叉树中序遍历
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 static int treeSize = 0;
public static void size(Node root) {
if (root == null) {
return;
}
treeSize++;
size(root.left);
size(root.right);
}
一种方法呢,是通过设一个静态变量,通过对二叉树进行遍历对它进行加减操作来计算结点个数。首先判断根节点是否为空,为空直接返回,否则对变量进行加加操作,然后利用递归,去遍历她的左子树,和右子树,从而最后变量的结果,就是结点个数。
public static int size(Node root) {
if (root == null) {
return 0;
}
return 1+size(root.left)+size(root.right);
}
第二种方法呢,就是不用静态变量,直接返回当前二叉树得结点个数。任然需要判断是否为空树,之后利用递归思想,树的结点个数,就是头节点加左子树结点个数和右子树结点个数,也就是1+左子树结点个数+右子树结点个数,而左子树和右子树,也可以用这个公式再去计算,所以便可以通过递归,从而直接返回结点个数。
四.求叶子节点个数
public static int leafSize = 0;
public static void leafSize(Node root) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
leafSize++;
return;
}
leafSize(root.left);
leafSize(root.right);
}
求叶子节点呢,也有同样的两种办法,先设置一个静态变量,先判断是否为空树,再去判断它的左右子树是否都为空,是的话他就是叶子节点,对变量进行加加操作,然后返回再去判断她的左子树和右子树。
public static int leafSize(Node root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return leafSize(root.left) + leafSize(root.right);
}
还有就是直接返回当前树的叶子节点个数,判断不为空后,直接判断是否为叶子节点,是的话返回1 ,最后直接返回左边的叶子节点和右边的叶子节点之和。
五.求第K层节点个数
public static int kLevelSize(Node root,int k) {
if (root == null || k<1) {
return 0;
}
if (k == 1) {
return 1;
}
return kLevelSize(root.left,k-1)+kLevelSize(root.right,k-1);
}
首先我们得判断是否为空树和K值是否合法,等于1的话,那就是头节点返回1,然后我们又分析出,第3层的节点个数=左子树的第2层节点数+右子树的第2层节点数,即(root,k) =( root.left,k-1 )+( root.right,k-1),然后用递归思想对左右子树进行操作。
六.寻找关键字为toFind的结点
public static Node result = null;
public static void find(Node root,char toFind) {
if (root == null) {
return;
}
if (root.val == toFind) {
result = root;
return;
}
find(root.left,toFind);
find(root.right,toFind);
}
这种方法任然是基于遍历,判断不为空树时,如果当前节点val为要找的,把他赋值给result,然后返回判断她的左右子树。
public static Node find(Node root,char toFind) {
if (root == null) {
return null;
}
if (root.val == toFind) {
return root;
}
Node result = null;
result = find(root.left,toFind);
if (result != null) {
return result;
}
return find(root.right,toFind);
}
还有是直接返回当前节点,如果当前头节点不是要找的,再去找她的左子树,找到的话赋给result,最后只需判断result是否为空,为空说明没找到,再去遍历右子树,若不为空,说明找到了,直接返回值。
七.oj面视题
import java.util.ArrayList;
import java.util.List;
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
public class TreeInterview {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;//空树要返回一个空的List,而不是null
}
result.add(root.val);
result.addAll(preorderTraversal(root.left));
result.addAll(preorderTraversal(root.right));
}
}
用List类型返回二叉树的先序遍历结果。先创建一个类,为空直接返回当前类,不为空,就把当前头节点值添加到result中,注意addAll操作,是吧参数中所有的值加入到当前类中。