【算法总结】你真的掌握了二叉树的遍历嘛(1),android开发游戏app

4    dfs(root.right);

5}

③ 后序

1void dfs(TreeNode root) {

2    dfs(root.left);

3    dfs(root.right);

4    visit(root);

5}

1. 非递归实现二叉树的前序遍历


144. Binary Tree Preorder Traversal (Medium)

Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/

1class Solution {

2    //迭代

3    public List preorderTraversal(TreeNode root) {

4        List res = new ArrayList();

5        Stack stack = new Stack();

6        while(root!=null || !stack.empty()){

7            while(root!=null){

8                res.add(root.val); //先将节点加入结果队列

9                stack.push(root);  //不断将该节点左子树入栈

10                root = root.left;

11            }

12            root = stack.pop(); //栈顶节点出栈

13            root = root.right; //转向该节点右子树的左子树(下一个循环)

14        }

15        return res;

16    }

17

18}

2. 非递归实现二叉树的中序遍历


94. Binary Tree Inorder Traversal (Medium)

Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/

1class Solution {

2    //迭代

3    public List preorderTraversal(TreeNode root) {

4        List res = new ArrayList();

5        Stack stack = new Stack();

6        while(root!=null || !stack.empty()){

7            while(root!=null){

8                stack.push(root);  //不断将该节点左子树入栈

9                root = root.left;

10            }

11            root = stack.pop(); //栈顶节点出栈

12            res.add(root.val); //将节点加入结果队列

13            root = root.right; //转向该节点右子树的左子树(下一个循环)

14        }

15        return res;

16    }

17}

3. 非递归实现二叉树的后序遍历


145. Binary Tree Postorder Traversal (Medium)

Leetcode / 力扣:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/

前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那
么这个顺序就和后序遍历正好相反。

1//修改前序遍历代码中,节点写入结果链表的代码:将插入队尾修改为插入队首

2//修改前序遍历代码中,每次先查看左节点再查看右节点的逻辑:变为先查看右节点再查看左节点

3class Solution {

4    public List postorderTraversal(TreeNode root) {

5        LinkedList res = new LinkedList();

6        Stack stack = new Stack<>();

7        TreeNode pre = null;

8        while(root!=null || !stack.empty()){

9            while(root!=null){

10                res.addFirst(root.val); //插入队首

11                stack.push(root);

12                root = root.right; //先右后左

13            }

14            root = stack.pop();

15            root = root.left;

16        }

17        return res;

18    }

19}

4. 非递归实现二叉树的层序遍历


leetcode:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/?spm=a2c4e.10696291.0.0.5e5719a42W3zNP

1class Solution {

2    public List<List> levelOrderBottom(TreeNode root) {

3        List<List> res = new LinkedList<>();

4        Queue queue = new LinkedList<>();

5        if(root == null)

6            return res;

7        queue.add(root);

8        while(!queue.isEmpty()){

9            int count = queue.size();

10            List temp = new LinkedList<>();

11            for(int i=0; i<count; i++){

12                TreeNode node = queue.poll();

13                temp.add(node.val);

14                if(node.left != null)

15                    queue.add(node.left);

16                if(node.right != null)

17                    queue.add(node.right);

18            }

19            // 每次都添加到第一个位置

20            res.add(0, temp);

21        }

22        return res;

23    }

24}

小结

  1. 递归实现二叉树的前中后遍历,这种方式非常简单,大家一定要掌握

  2. 非递归的方式,其实也不难,前中后序遍历方式主要借助栈的特征,层序遍历的方式主要借助队列的方式,大家也要掌握,多敲两遍,就记住了。

18            }

19            // 每次都添加到第一个位置

20            res.add(0, temp);

21        }

22        return res;

23    }

24}

小结

  1. 递归实现二叉树的前中后遍历,这种方式非常简单,大家一定要掌握

  2. 非递归的方式,其实也不难,前中后序遍历方式主要借助栈的特征,层序遍历的方式主要借助队列的方式,大家也要掌握,多敲两遍,就记住了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值