• 算法通关村第七关-黄金挑战二叉树迭代遍历


    大家好我是苏麟 , 今天带来二叉树的迭代遍历 .

    二叉树的迭代遍历

    前序遍历

    描述 :

    给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

    题目 :

    LeetCode 二叉树的前序遍历 :

    144. 二叉树的前序遍历

    分析 :

    前序遍历是中左右,如果还有左子树就一直向下找。完了之后再返回从最底层逐步向上向右找。 不难写出如下代码 :

    解析 :

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public List preorderTraversal(TreeNode root) {
    18. List list = new ArrayList<>();
    19. if(root == null){
    20. return list;
    21. }
    22. Stack stack = new Stack<>();
    23. TreeNode temp = root;
    24. while(!stack.isEmpty() || temp != null){
    25. while(temp != null){
    26. stack.add(temp);
    27. list.add(temp.val);
    28. temp = temp.left;
    29. }
    30. temp = stack.pop();
    31. temp = temp.right;
    32. }
    33. return list;
    34. }
    35. }

    中序遍历

    描述 :

    给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

    题目 :

    LeetCode 94.二叉树的中序遍历 :

    94. 二叉树的中序遍历

    分析 :

    中序遍历,中序遍历是左中右,先访问的是二又树左子树的节点,然后一层一层向下访问,直到到达树左面的最底部,再开始处理节点(也就是在把节点的数值放进res列表中)。在使用迭代法写中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来处理节点上的元素。 

    解析 :

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public List inorderTraversal(TreeNode root) {
    18. List list = new ArrayList<>();
    19. if(root == null){
    20. return list;
    21. }
    22. Stack stack = new Stack<>();
    23. TreeNode node = root;
    24. while(!stack.isEmpty() || node != null){
    25. while(node != null){
    26. stack.push(node);
    27. node = node.left;
    28. }
    29. node = stack.pop();
    30. list.add(node.val);
    31. node = node.right;
    32. }
    33. return list;
    34. }
    35. }

    后序遍历

    描述 :

    给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 

    题目:

    LeetCode 145.二叉树的后序遍历 :

    145. 二叉树的后序遍历

    分析 :

    后序遍历,后序遍历是左右中,先访问的是二又树左子树的节点,然后一层一层向下访问,直到到达树左面的最底部,再开始处理节点(也就是在把节点的数值放进res列表中)。在使用迭代法写后序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来处理节点上的元素。 

    解析 :

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public List postorderTraversal(TreeNode root) {
    18. List list = new ArrayList<>();
    19. if(root == null){
    20. return list;
    21. }
    22. Stack stack = new Stack<>();
    23. TreeNode node = root;
    24. while(!stack.isEmpty() || node != null){
    25. while(node != null){
    26. list.add(node.val);
    27. stack.push(node);
    28. node = node.right;
    29. }
    30. node = stack.pop();
    31. node = node.left;
    32. }
    33. Collections.reverse(list);
    34. return list;
    35. }
    36. }

    这期就到这里 , 下期再见 !

  • 相关阅读:
    如何为项目匹配资源技能和要求?
    ICE安全插件配置实操
    C++多态
    第二章:Pythonocc官方demo 案例44(几何板条)
    Redis企业版数据库如何支持实时金融服务?
    解决Java异常java.sql.SQLException: Unexpected exception encountered during query.
    Linux系统——SElinux
    Android12启动页适配
    从意义中恢复,而不是从数据包中恢复
    猿创征文|【C++之new和delete运算符】创建数组
  • 原文地址:https://blog.csdn.net/sytdsqzr/article/details/134310551