• 树的遍历


    递归版本:

    /*
    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 } }
  • 相关阅读:
    python 希尔排序
    python 选择排序
    python 插入排序
    怎么根据返回数量,来渲染点赞星星的个数变高量?
    avalon $computed不起作用?
    移动端点击事件兼容问题,在pc端可以点,在手机上不可以点
    移动端弹出遮罩层禁止页面滚动,遮罩里面的框允许滚动如何实现。
    移动端,如何禁止弹窗下面内容滚动/页面滚动
    avalon数据已更新,视图未更新的bug修复
    Python 导入分部分项前的 数据处理
  • 原文地址:https://www.cnblogs.com/team42/p/7003333.html
Copyright © 2020-2023  润新知