二叉树的遍历

二叉树定义

public class ThreeNode {

     public int value;
     public ThreeNode rigth;
     public ThreeNode left;
     public ThreeNode() {
     }
     public ThreeNode(int value) {
           super();
           this.value = value;
     }
     public ThreeNode(int value, ThreeNode rigth, ThreeNode left) {
           super();
           this.value = value;
           this.rigth = rigth;
           this.left = left;
     }    
}    

递归

前序

     //前序遍历-递归版本 中左右
     public static List<Integer> preOrderTraverse(ThreeNode root){

           List<Integer> result = new ArrayList<>();

   
           if (root == null) {
                return result;

           }
           List<Integer> left = preOrderTraverse(root.left);
           List<Integer> right = preOrderTraverse(root.rigth);

           result.add(root.value);
           result.addAll(left);
           result.addAll(right);
           return result;

}

中序

//中序遍历-递归版本 左中右
     public static List<Integer> innerOrderTraverse(ThreeNode root){

           List<Integer> result = new ArrayList<>();

          
           if (root == null) {
                return result;
           }

           

           List<Integer> left = innerOrderTraverse(root.left);
           List<Integer> right = innerOrderTraverse(root.rigth);

           result.addAll(left);
           result.add(root.value);
           result.addAll(right)return result;

     }


后序

     //后序遍历-递归版本 左右中
     public static List<Integer> postOrderTraverse(ThreeNode root){

           List<Integer> result = new ArrayList<>();

           if (root == null) {
                return result;
           }
           List<Integer> left = postOrderTraverse(root.left);
           List<Integer> right = postOrderTraverse(root.rigth);

          
           result.addAll(left);
           result.addAll(right);
           result.add(root.value);
           return result;

     }

非递归

前序

     //前序遍历-非递归版本
     public static List<Integer> preOrderTraverseNot(ThreeNode root){
           List<Integer> result = new ArrayList<>();
           if (root == null) {
                return result;
           }
           Stack<ThreeNode> stack = new Stack<>();
           stack.push(root);
           ThreeNode current = null;

           while(!stack.isEmpty()) {
                current = stack.pop();
                result.add(current.value);
                if (current.rigth != null) {
                      stack.push(current.rigth);
                }

                if (current.left!=null) {
                      stack.push(current.left);
                }
           }
           return result;
     }

中序

     //中序遍历-非递归版本

     public static List<Integer> innerOrderTraverseNot(ThreeNode root){
           List<Integer> result = new ArrayList<>();
           if (root == null) {
                return result;
           }
           Stack<ThreeNode> stack = new Stack<>();
           ThreeNode current = root;
           while(current !=null || !stack.isEmpty()) {
                while(current != null) {
                      stack.push(current);
                      current = current.left;
                }
                current = stack.pop();
                result.add(current.value);
                current = current.rigth;
           }
           return result;
     }

后序

//后序遍历-非递归 左右中 改变了原始树的数据结构

     public static List<Integer> postOrderTraverseNo(ThreeNode root){

           List<Integer> result = new ArrayList<>();
           if (root == null) {
                return result;
           }
           Stack<ThreeNode> stack = new Stack<>();
           stack.push(root);
           while(!stack.isEmpty()) {
                ThreeNode node = stack.peek();
                if (node.rigth == null && node.left==null) {
                      result.add(node.value);
                }
                if (node.rigth!=null) {
                      stack.push(node.rigth);
                      node.rigth = null;
                }
                if (node.left != null) {
                      stack.push(node.left);
                      node.left = null;

                }
           }
           return result;
     }
     //后序遍历-非递归 左右中 

     public static List<Integer> postOrderTraverseNo2(ThreeNode root){

           List<Integer> result = new ArrayList<>();
           if (root == null) {
                return result;
           }

           Stack<ThreeNode> stack = new Stack<>();
           ThreeNode prev = null;
           ThreeNode current = root; 
           stack.push(root);

           while(!stack.isEmpty()) {

                current = stack.peek();

                if (prev == null || prev.left == current || prev.rigth == current) {
                      if (current.left != null) {
                           stack.push(current.left);
                      }else if(current.rigth!=null){
                           stack.push(current.rigth);
                      }
                }else if (current.left == prev) {
                      if (current.rigth != null ) {
                           stack.push(current.rigth);
                      }
                }else {
                      result.add(current.value);
                      stack.pop();
                }
                prev = current;
           }
           return result;
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值