二叉树的常见操作
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;import javax.swing.LayoutStyle;
import java.util.ArrayList;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
this.val=val;
left=null;
right=null;
}
}
public class TreeSummary {
//递归中序
public static void inorder(TreeNode root){
if(root==null){
return;
}
inorder(root.left);
System.out.println(root.val);
inorder(root.right);
}
//递归前序
public static void preOrder(TreeNode root){
if(root!=null){
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
//递归后序
public static void postOrder(TreeNode root){
if(root!=null){
postOrder(root.left);
postOrder(root.right);
System.out.println(root.val);
}
}
//非递归前序遍历
public static void norepreOrder(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode node=root;
while(node!=null||!stack.isEmpty()){
while(node!=null){
System.out.println(node.val);
stack.push(node);
node=node.left;
}
node=stack.pop();
node=node.right;
}
}
//非递归中序遍历
public static void noreinOrder(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode node=root;
while(node!=null||!stack.isEmpty()){
while(node!=null){
stack.push(node);
node=node.left;
}
if(!stack.isEmpty()){
node=stack.pop();
System.out.println(node.val);
node=node.right;
}
}
}
//非递归后序遍历
public static void norepostOrder(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode node=root;
// 左子树入栈
for (; root.left != null; root = root.left)
stack.push(root.left);
while(root!=null&&(root.right==null||root.right==node)){
System.out.println(root);
node=root;
if (stack.empty())
return;
root = stack.pop();
}
//处理右子树
stack.push(root);
root=root.right;
}
public static int getheight(TreeNode root){
if(root==null){
return 0;
}
else{
int i=getheight(root.left);
int j=getheight(root.right);
return (i<j)?(j+1):(i+1);
}
}
// 非递归后序遍历
public static void postorderTraversal(TreeNode root) {
Stack<TreeNode> treeNodeStack = new Stack<TreeNode>();
TreeNode node = root;
TreeNode lastVisit = root;
while (node != null || !treeNodeStack.isEmpty()) {
while (node != null) {
treeNodeStack.push(node);
node = node.left;
}
//查看当前栈顶元素
node = treeNodeStack.peek();
//如果其右子树也为空,或者右子树已经访问
//则可以直接输出当前节点的值
if (node.right == null || node.right == lastVisit) {
System.out.print(node.val + " ");
treeNodeStack.pop();
lastVisit = node;
node = null;
} else {
//否则,继续遍历右子树
node = node.right;
}
}
}
//层次遍历
public static void bfs(TreeNode root){
Queue<TreeNode> queue=new LinkedList<TreeNode>();
TreeNode node=root;
while((node!=null)||(!queue.isEmpty())){
if(node!=null){
System.out.println(node.val);
queue.add(node.left);
queue.add(node.right);
node=queue.poll();
}
else{
node=queue.poll();
}
}
}
//搜索二叉树的第K个节点
TreeNode KthNode(TreeNode pRoot, int k)
{
if(k<=0 || pRoot==null )
return null;
ArrayList<TreeNode> list=new ArrayList<TreeNode>();
inorder(pRoot,list);
if(k>list.size())
return null;
return list.get(k-1);
}
public void inorder (TreeNode head,ArrayList<TreeNode> list){
if(head==null)
return;
inorder(head.left,list);
list.add(head);
inorder(head.right,list);
}
//判断是否是平衡二叉树
public boolean IsBalanced_Solution(TreeNode root){
if (root==null){
return true;
}
int leftheight=getHeight(root.left);
int rightheight=getHeight(root.right);
if(Math.abs(leftheight-rightheight)>1)
return false;
else{
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
}
}
public int getHeight(TreeNode root){
if(root==null)
return 0;
int leftHeight=getHeight(root.left);
int rightHeight=getHeight(root.right);
return rightHeight>leftHeight?rightHeight+1:leftHeight+1;
}
}