java 144.二叉树Preorder Traversal(Morris).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 144.二叉树Preorder Traversal(Morris).java相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
 // eg:[10,5,30,-2,6,12,40,null,2,null,null,null,null,null,null,-1]
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<Integer>();
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        
        TreeNode cur = root;
        while( cur != null || !stack.isEmpty()){
            while( cur != null){
                res.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }

            cur = stack.pop();
            cur = cur.right;
        }
        return res;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
 // eg:[10,5,30,-2,6,12,40,null,2,null,null,null,null,null,null,-1]
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<Integer>();
        
        helper(root,res);
        
        return res;
    }
    
    public void helper(TreeNode root, List<Integer> res){
        if (root == null) return;
        res.add(root.val);
        helper(root.left, res);
        helper(root.right, res);
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
 // eg:[10,5,30,-2,6,12,40,null,2,null,null,null,null,null,null,-1]
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<Integer>();
        
        TreeNode cur = root;
        
        while( cur != null) {
            
            if (cur.left == null){
                res.add(cur.val);
                cur = cur.right;
            } else {
                TreeNode temp = cur.left;
                
                while( temp.right != null && temp.right != cur){
                    temp = temp.right;
                }
                
                if(temp.right == null){
                    temp.right = cur;
                    res.add(cur.val);
                    cur = cur.left;
                } else {
                    temp.right = null;
                    cur = cur.right;
                }
            }
            
        }
        
        return res;
    }
}

以上是关于java 144.二叉树Preorder Traversal(Morris).java的主要内容,如果未能解决你的问题,请参考以下文章

java 144.二叉树Preorder Traversal(Morris).java

java 144.二叉树Preorder Traversal(Morris).java

java 144.二叉树Preorder Traversal(Morris).java

java 144.二叉树Preorder Traversal(Morris).java

java 144.二叉树Preorder Traversal(Morris).java

java 144.二叉树Preorder Traversal(Morris).java