Leetcode - Binary Tree Preorder Traversal

Leetcode - Binary Tree Preorder Traversal_第1张图片

My code:

import java.util.ArrayList;
import java.util.List;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List preorderTraversal(TreeNode root) {
        ArrayList result = new ArrayList();
        if (root == null)
            return result;
        preorder(root, result);
        return result;
    }
    
    private void preorder(TreeNode root, ArrayList result) {
        result.add(root.val);
        if (root.left != null)
            preorder(root.left, result);
        if (root.right != null)
            preorder(root.right, result);
    }
        
}

My test result:

Leetcode - Binary Tree Preorder Traversal_第2张图片

考先序遍历的,没什么好说的。

非递归写法:

    public List preOrder(TreeNode root) {
        if (root == null)
            return null;
        TreeNode p = root;
        Stack s = new Stack();
        ArrayList result = new ArrayList();
        while (p != null || !s.isEmpty()) {
            while (p != null) {
                result.add(p);
                s.push(p);
                p = p.left;
            }
            
            if (!s.isEmpty()) {
                p = s.pop();
                p = p.right;        
            }
        }
        return result;
    }

**
总结: pre order tree
**

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List preorderTraversal(TreeNode root) {
        ArrayList ret = new ArrayList();
        if (root == null)
            return ret;
        Stack s = new Stack();
        TreeNode node = root;
        while (node != null) {
            ret.add(node.val);
            if (node.right != null)
                s.push(node.right);
            node = node.left;
            if (node == null && !s.isEmpty()) {
                node = s.pop();
            }
        }
        return ret;
    }
}

这是我的写法。就是每次访问当前结点,然后如果right child != null, 压入栈中。
curr = curr.left;
然后判断 curr 是否为 null.如果是,表示走到头了。那就从栈中弹出元素,开始访问右侧。
注意循环判断条件得是 node != null 而不是栈是否为空。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List preorderTraversal(TreeNode root) {
        List ret = new ArrayList();
        if (root == null) {
            return ret;
        }
        Stack st = new Stack();
        TreeNode p = root;
        while (p != null || !st.isEmpty()) {
            while (p != null) {
                ret.add(p.val);
                st.push(p);
                p = p.left;
            }
            p = st.pop().right;
        }
        
        return ret;
    }
}

复习了下 iteration的写法,但还是没能写出来,看了答案。

Anyway, Good luck, Richardo! -- 09/06/2016

Morris traversal:

My code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List preorderTraversal(TreeNode root) {
        List ret = new ArrayList();
        if (root == null) {
            return ret;
        }
        
        TreeNode curr = root;
        while (curr != null) {
            if (curr.left == null) {
                ret.add(curr.val);
                curr = curr.right;
            }
            else {
                TreeNode pre = curr.left;
                while (pre.right != null && pre.right != curr) {
                    pre = pre.right;
                }
                
                if (pre.right == null) {
                    ret.add(curr.val);
                    pre.right = curr;
                    curr = curr.left;
                }
                else {
                    pre.right = null;
                    curr = curr.right;
                }
            }
        }
        
        return ret;
    }
}

reference:
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html

Anyway, Good luck, Richardo! -- 09/08/2016

你可能感兴趣的:(Leetcode - Binary Tree Preorder Traversal)