二叉树的定义
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
public TreeNode() {
}
}
1,重建
public static TreeNode buildTree(int[] preOrder,
int start,int[] inOrder,int end,int length){
if(preOrder==null||preOrder.length==0
||inOrder==null||inOrder.length==0||length<=0){
return null;
}
int value=preOrder[start];
TreeNode root=new TreeNode(value);
if(length==1){
return root;
}
int i=0;
while(i<length){
if(value==inOrder[end-i]){
break;
}
i++;
}
root.left=buildTree(preOrder,start+1,inOrder,end-1-i,length-1-i);
root.right=buildTree(preOrder,start+length-i,inOrder,end,i);
return root;
}
2, 前序遍历
public static void preorderTraversal(TreeNode root){
if(root==null) return ;
System.out.print(root.val+" ");
inorderTraversal(root.left);
inorderTraversal(root.right);
}
public static List<TreeNode> preorderTraversal2(TreeNode root){
List<TreeNode> tList=new ArrayList<TreeNode>();
Stack<TreeNode> tStack=new Stack<TreeNode>();
tStack.push(root);
while(!tStack.isEmpty()){
TreeNode p=tStack.pop();
tList.add(p);
System.out.print(p.val+" ");
if(p.right!=null) tStack.push(p.right);
if(p.left!=null) tStack.push(p.left);
}
return tList;
}
3,中序遍历
public static void inorderTraversal(TreeNode root){
if(root==null) return ;
inorderTraversal(root.left);
System.out.print(root.val+" ");
inorderTraversal(root.right);
}
public static List<TreeNode> inorderTraversal2(TreeNode root){
List<TreeNode> tList=new ArrayList<TreeNode>();
Stack<TreeNode> tStack=new Stack<TreeNode>();
TreeNode p=root;
while(p!=null || !tStack.isEmpty()){
if(p!=null){
tStack.push(p);
p=p.left;
}
else{
p=tStack.pop();
tList.add(p);
p=p.right;
}
}
return tList;
}
4,后序遍历
public static void postorderTraversal(TreeNode root){
if(root==null) return ;
inorderTraversal(root.left);
inorderTraversal(root.right);
System.out.print(root.val+" ");
}
5,层序遍历
public static void levelTravel(TreeNode root){
if(root==null) return;
Queue<TreeNode> q=new LinkedList<TreeNode>();
q.add(root);
while(!q.isEmpty()){
TreeNode p=q.poll();
System.out.println(p.val);
if(p.left!=null) q.add(p.left);
if(p.right!=null) q.add(p.right);
}
}
public List<List<Integer>> levelOrder(TreeNode root){
List<List<Integer>> list=new LinkedList<List<Integer>>();
if(root==null ) return list;
Queue<TreeNode> currentLevel=new LinkedList<TreeNode>();
currentLevel.add(root);
while(!currentLevel.isEmpty()){
List<Integer> currentList=new LinkedList<Integer>();
for(int i=0;i<currentLevel.size();i++){
TreeNode currentNode=currentLevel.poll();
currentList.add(currentNode.val);
if(currentNode.left!=null)
currentLevel.add(currentNode.left);
if(currentNode.right!=null)
currentLevel.add(currentNode.right);
}
list.add(currentList);
}
return list;
}
6,深度
public static int depthOfBinaryTree(TreeNode root){
if(root==null)
return 0;
return Math.max(depthOfBinaryTree(root.left), depthOfBinaryTree(root.right))+1;
}
7,平衡二叉树的判定
public static boolean isBalance(TreeNode root){
if(root==null)
return true;
if(Math.abs(depthOfBinaryTree(root.left)-depthOfBinaryTree(root.right))>1){
return false;
}
return isBalance(root.left)&&isBalance(root.right);
}
8,路径和为某特定值
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null)
return false;
if(root.left==null && root.right==null && root.val==sum)
return true;
return hasPathSum(root.left,sum-root.val) ||hasPathSum(root.right,sum-root.val);
}
9,到叶子节点的最短路径。
public int minDepth(TreeNode root) {
if(root==null) return 0;
int left=minDepth(root.left);
int right=minDepth(root.right);
if(left==0 && right==0)
return 1;
if(left==0)
left=Integer.MAX_VALUE;
if(right==0)
right=Integer.MAX_VALUE;
return Math.min(left, right)+1;
}