二叉树中的遍历(迭代与递归)

二叉树的种类

满二叉树

深度为k,有2^k-1个节点的二叉树。只有度为0和度为2的节点,度=2节点都在同一行

完全二叉树:除了最底层节点没填满,其余层节点数达到最大值,最底层节点集中在最左边的位置。如下图

二叉搜索树

若左子树不为空,左子树上节点都小于根结点的值

若右子树不为空,右子树节点都大于根节点的值

左右子树也是二叉搜索树

平衡二叉树:AVL

性质:1)空树;

          2) 左右子树高度差不超过1;

迭代与递归

1递归:自己调用自己,利用上下文栈保存信息

2.迭代:利用循环结构,重复执行代码块,不需要额外的栈开销

前序遍历

遍历顺序:中、左、右

144. 二叉树的前序遍历 - 力扣(LeetCode)

1.递归

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List <Integer> alist=new ArrayList<>();
        firstbianli(root,alist);
        return alist;
        
    }
//递归
    public void firstbianli(TreeNode root ,List<Integer> list){
      
        if(root!=null){ 
		 list.add(root.val);
        firstbianli(root.left,list);
        firstbianli(root.right,list);
        }
    }
}

2.迭代

这里数据结构采用的是栈,先进后出,所以这里先遍历右节点 再遍历左节点 

//迭代查询

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List <Integer> alist=new ArrayList<>();
    //LinkedList 中调用push()、pop()==栈
          LinkedList<TreeNode> stack =new LinkedList<>();
         if(root==null) return alist;
           stack.push(root);

   //if(root!=null) stack.push(root);

            while(!stack.empty()){

        TreeNode cur=stack.pop();
        alist.add(cur.val);//中间节点
        if(cur.right!=null){
            stack.push(cur.right);//右节点入栈
        }
        if(cur.left!=null){
            stack.push(cur.left);//左节点入栈
        }    

    }
        return alist;
    }
   
}

中序遍历

遍历顺序:左、中、右

1.递归

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List <Integer> alist=new ArrayList<>();
        Secondbianli(root,alist);
        return alist;
        
    }
    public void Secondbianli(TreeNode root ,List<Integer> list){
        
        if(root!=null){ 
         Secondbianli(root.left,list);
		 list.add(root.val);
        Secondbianli(root.right,list);
        }
    }
}

2.迭代

思路:左节点非空入栈,再去找左节点,入栈,直到左节点为空,空就,将该节点出栈,再将该节点的右节点入栈


class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
      
       List<Integer> list=new ArrayList<>();
       LinkedList<TreeNode> stack =new LinkedList<>();
       
       TreeNode cur=root;//指针
   
       while(cur!=null||stack.empty()!=true){
        if(cur!=null){
            stack.push(cur);
            cur=cur.left;
        }
        else{
           cur= stack.pop();
            list.add(cur.val);
            cur=cur.right;
        }
       }return list;
       
    }
}

后序遍历

1.递归

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List <Integer> alist=new ArrayList<>();
        Lastbianli(root,alist);
        return alist;
        
    }
    public void Lastbianli(TreeNode root ,List<Integer> list){
        
        if(root!=null){ 
		
        Lastbianli(root.left,list);
        Lastbianli(root.right,list);
        list.add(root.val);
        }
    }
}

2.迭代

出栈顺序:中、右、左

反转:左、右、中

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list =new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root!=null) stack.push(root);
        while(stack.empty()!=true){
            TreeNode cur=stack.pop();
            list.add(cur.val);//中间节点
            if(cur.left!=null) stack.push(cur.left);//左节点入栈,先进后出
            if(cur.right!=null) stack.push(cur.right);//右节点入栈
        }
        Collections.reverse(list);
        return list;//反转非常重要!!!!
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值