码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]145 Binary Tree Postorder Traversal

时间:2015-01-02 07:33:36      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/binary-tree-postorder-traversal/

http://blog.csdn.net/ljphhj/article/details/21369053


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        
        // Solution A
        // List<Integer> toReturn = new ArrayList<>();
        // postorderTraversal_recursively(root, toReturn);
        // return toReturn;
        
        // Solution B
        return postorderTraversal_iteratively(root);
    }

    ///////////////////////
    //
    // Solution B: Iteratively
    //
    private List<Integer> postorderTraversal_iteratively(TreeNode root)
    {
        List<Integer> toReturn = new ArrayList<>();
        if (root == null)
            return toReturn;
        
        // Define a stack.
        Stack<CheckedNode> stack = new Stack<>();
        stack.push(new CheckedNode(root, false));
        
        while (!stack.empty())
        {
            CheckedNode checkednode = stack.pop();
            
            if (checkednode.checked ||  // The node has been checked
               (checkednode.node.left == null && checkednode.node.right == null)) // A leaf node
            {
                // If the node has checked
                // Just add its value
                toReturn.add(checkednode.node.val);
            }
            else
            {
                // Checking this node.
                // Push it back, push its right, push its left
                checkednode.checked = true;
                stack.push(checkednode);
                if (checkednode.node.right != null)
                {
                    stack.push(new CheckedNode(checkednode.node.right, false));
                }
                if (checkednode.node.left != null)
                {
                    stack.push(new CheckedNode(checkednode.node.left, false));
                }
            }
        }
        return toReturn;
    }
    
    private static class CheckedNode
    {
        TreeNode node;
        boolean checked;
        CheckedNode(TreeNode node, boolean checked)
        {
            this.node = node;
            this.checked = checked;
        }
    }

    
    ///////////////////////
    //
    // Solution A: Recursively
    //
    
    private void postorderTraversal_recursively(TreeNode node, List<Integer> result)
    {
        if (node == null)
            return;
            
        postorderTraversal_recursively(node.left, result);
        postorderTraversal_recursively(node.right, result);
        result.add(node.val);
    }
}


[LeetCode]145 Binary Tree Postorder Traversal

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1598371

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!