美文网首页
二叉树非递归遍历——已通过LintCode

二叉树非递归遍历——已通过LintCode

作者: gustiness | 来源:发表于2018-04-14 07:55 被阅读0次

先序遍历

LintCode题目链接

public class Solution {
    /**
     * @param root: A Tree
     * @return: Preorder in ArrayList which contains node values.
     */
    public List<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        
        while(root != null || !stack.empty()){
            if(root == null){
                root = stack.peek();
                stack.pop();
            }else{
                result.add(root.val);
                if(root.right != null){
                    stack.push(root.right);
                }
                root = root.left;
            }
            
        }
        
        return result;
    }
}

中序遍历

LintCode题目链接

public class Solution {
    /**
     * @param root: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        
        if(root == null){
            return result;
        }
        
        while(root != null || !stack.empty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            
            if(!stack.empty()){
                root = stack.peek();
                result.add(root.val);
                stack.pop();
                root = root.right;
            }
        }
        return result;
    }
}

后序遍历

LintCode题目链接
由于在LintCode的代码里定义内部类会出现奇怪的问题,不得已使用了两个Stack。nodeStack用于存放树节点,flagStack用来存放对应节点是否为第一次访问。

public class Solution {
    
    /**
     * @param root: A Tree
     * @return: Postorder in ArrayList which contains node values.
     */
     
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> nodeStack = new Stack<>();
        Stack<Boolean> flagStack = new Stack<>();

        
        while(root != null || !nodeStack.empty()){
            while(root != null){
                nodeStack.push(root);
                flagStack.push(true);
                root = root.left;
            }
            
            if(!nodeStack.empty()){
                TreeNode node = nodeStack.peek();
                Boolean flag = flagStack.peek();
                
                if(flag){
                    flagStack.pop();
                    flagStack.push(false);
                    root = node.right;
                }else{
                    result.add(node.val);
                    nodeStack.pop();
                    flagStack.pop();
                    root = null;
                }
            }
        }
        return result;
        
    }
}

当做一个记录,如果大家需要解释思路的话,不妨留言,我再补上思路~

相关文章

  • 二叉树遍历java,非递归、层次。

    /** * 前序遍历 * 递归 */ /*** 前序遍历* 非递归*/ 后续遍历非递归 二叉树层次遍历基于java...

  • 二叉树非递归遍历——已通过LintCode

    先序遍历 LintCode题目链接 中序遍历 LintCode题目链接 后序遍历 LintCode题目链接由于在L...

  • 二叉树递归非递归遍历算法整理

    一、二叉树前序遍历 1 前序递归遍历 2.前序非递归遍历 一、二叉树中序遍历 2.中序递归遍历 1.中序非递归遍历...

  • 总结

    1、二叉树广度遍历(非递归) 广度遍历非递归实现需要依靠一个队列。 2、二叉树深度遍历(递归与非递归,前序,中序和...

  • 二叉树遍历

    二叉树的遍历 1. 前序遍历 1.1 递归前序遍历 1.2 非递归前序遍历 2 中序遍历 2.1递归遍历 2.2非...

  • 二叉树,非递归法

    递归法 二叉树的递归,有前序遍历、中序遍历、后序遍历,一般采用递归法,比较简单 非递归法 二叉树非递归法,采用栈来实现

  • 二叉树遍历-JAVA实现

    基础二叉树 二叉树遍历分为前序、中序、后序递归和非递归遍历、还有层序遍历。 前序递归遍历算法:访问根结点-->递归...

  • 算法之二叉树

    二叉树之C++实现 创建二叉树 复制二叉树 先序遍历 递归实现 非递归实现 中序遍历 递归实现 非递归实现 后序遍...

  • 二叉树遍历

    请递归,非递归方式分别前序遍历,中序遍历,后续遍历二叉树

  • 二叉树

    结构体 创建二叉树 递归遍历 栈操作 非递归遍历 层次遍历 完整代码

网友评论

      本文标题:二叉树非递归遍历——已通过LintCode

      本文链接:https://www.haomeiwen.com/subject/lcpkkftx.html