• 二叉树的迭代遍历统一写法


    94.二叉树的中序遍历

    具体实现:

    将访问的节点直接加入到栈中,但如果是出去过的节点则后面再放入一个空节点,

    这样只有空节点弹出的时候,才将下一个节点放进结果集。

    代码:

    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            Deque<TreeNode> stack = new LinkedList<>();
            TreeNode cur = root;
            if(cur != null) stack.push(cur);
            while (!stack.isEmpty()) {
                cur = stack.peek();
                if (cur != null) {
                    cur = stack.pop();
                    if (cur.right != null) stack.push(cur.right);
                    stack.push(cur);
                    stack.push(null);
                    if (cur.left != null) stack.push(cur.left);
                } else {
                    stack.pop();
                    cur = stack.pop();
                    result.add(cur.val);
                }
            }
            return result;
        }
    }

    144.二叉树的前序遍历

    代码:

    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            Deque<TreeNode> stack = new LinkedList<>();
            TreeNode cur = root;
            if(cur != null) stack.push(cur);
            while (!stack.isEmpty()) {
                cur = stack.peek();
                if (cur != null) {
                    cur = stack.pop();
                    if (cur.right != null) stack.push(cur.right);
                    if (cur.left != null) stack.push(cur.left);
                    stack.push(cur);
                    stack.push(null);
                } else {
                    stack.pop();
                    cur = stack.pop();
                    result.add(cur.val);
                }
            }
            return result;
        }
    }

    145.二叉树的后序遍历

    代码:

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            Deque<TreeNode> stack = new LinkedList<>();
            TreeNode cur = root;
            if(cur != null) stack.push(cur);
            while (!stack.isEmpty()) {
                cur = stack.peek();
                if (cur != null) {
                    cur = stack.pop();
                    stack.push(cur);
                    stack.push(null);
                    if (cur.right != null) stack.push(cur.right);
                    if (cur.left != null) stack.push(cur.left);
                    
                } else {
                    stack.pop();
                    cur = stack.pop();
                    result.add(cur.val);
                }
            }
            return result;
        }
    }
  • 相关阅读:
    选项卡自动切换(定时器demo)
    JS基础——选项卡套选项卡(函数传参)
    JS基础——修改文本框的值(函数传参)
    JS基础——选项卡列表显示隐藏缩略图(函数传参)
    js基础——图片切换实例(函数传参)
    JS 获取元素的属性值,非内联样式
    css position 定位
    ie6-7 overflow:hidden失效问题的解决方法
    Vue自带的过滤器
    Vue数据绑定
  • 原文地址:https://www.cnblogs.com/zhaojiayu/p/15816244.html
Copyright © 2020-2023  润新知