二叉树

二叉树的常见操作

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;

    } 



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值