zoukankan      html  css  js  c++  java
  • 树的遍历

    递归版本:

    /*
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }*/
    public class TreeToSequence {
        public int[][] convert(TreeNode root) {
            ArrayList<Integer> x=new ArrayList<Integer>();
            ArrayList<Integer> z=new ArrayList<Integer>();
            ArrayList<Integer> h=new ArrayList<Integer>();
            xian(x,root);
            zhong(z,root);
            hou(h,root);
            int[][] res=new int[3][x.size()];
            for(int i=0;i<x.size();i++){
                res[0][i]=x.get(i);
                res[1][i]=z.get(i);
                res[2][i]=h.get(i);
            }
            return res;
              
        }
        static void xian(ArrayList list,TreeNode root){
            if(root==null){
               return;
            }
            list.add(root.val);
            xian(list,root.left);
            xian(list,root.right);
        
        }
         static void zhong(ArrayList list,TreeNode root){
             if(root==null){
                return;
            }
            zhong(list,root.left);
            list.add(root.val);
               zhong(list,root.right);
         }
        static void hou(ArrayList list,TreeNode root){
            if(root==null){
              return;
            }
            hou(list,root.left);
              hou(list,root.right);
            list.add(root.val);
            
        }
    } 

    非递归/*public class TreeNode {    int val = 0;    TreeNode left = null;

        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }*/
    public class TreeToSequence {
        public int[][] convert(TreeNode root) {
             if(root == null){
                return null;
            }
            ArrayList<Integer> x = new ArrayList<Integer>();
            ArrayList<Integer> z = new ArrayList<Integer>();
            ArrayList<Integer> h = new ArrayList<Integer>();
            xian(x,root);
            zhong(z,root);
            hou(h,root);
            int[][] res=new int[3][x.size()];
             for(int i = 0; i < x.size(); i++){
                res[0][i] = x.get(i);
                res[1][i] = z.get(i);
                res[2][i] = h.get(i);
            }
            return res;
        }
       void xian(ArrayList<Integer> list,TreeNode root){
           if(root==null) return ;
           Stack<TreeNode> s=new Stack<TreeNode>();
           s.push(root);
           while(!s.isEmpty()){
               TreeNode cur=s.pop();
               list.add(cur.val);
               if(cur.right!=null)    //后进先出,所以先添加右孩子,再添加左孩子
                   s.push(cur.right);
               if(cur.left!=null)
                   s.push(cur.left);
           }
       }
        
    //中序其实就是最简单的回溯
    void zhong(ArrayList<Integer> list,TreeNode root){ if(root==null) return ; Stack<TreeNode> s=new Stack<TreeNode>(); TreeNode cur=root; while(!s.isEmpty()||cur!=null){ if(cur!=null){ s.push(cur); cur=cur.left; }else{ cur=s.pop(); list.add(cur.val); cur=cur.right; } } } void hou(ArrayList<Integer> list,TreeNode root){ if(root==null) return ; Stack<TreeNode> s=new Stack<TreeNode>();
        
    s.push(root); while(!s.isEmpty()){ TreeNode cur=s.pop(); if(cur.left!=null){ s.push(cur.left); } if(cur.right!=null){ s.push(cur.right); } list.add(cur); } Collections.reverse(list)l } }
  • 相关阅读:
    CentOS 7.1下KVM的安装与配置
    ORACLE常用命令
    linux下输出tomcat控制台信息
    express添加拦截器
    MySQL存储过程
    supervisor提高nodejs调试效率
    Eclipse Jetty调试时无法保存js文件
    eclipse读取含有extjs的项目文件时卡死
    springmvc4+hibernate4+activiti5.18(Maven)
    spring+jersey+c3p0构建restful webservice(数据源采用c3p0)
  • 原文地址:https://www.cnblogs.com/team42/p/7003333.html
Copyright © 2011-2022 走看看