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

[LeetCode] Binary Tree Preorder Traversal

时间:2014-09-05 22:17:42      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   div   sp   log   

public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
         List<Integer> result = new ArrayList<Integer>();
        Stack<TreeNode> nodeStack = new Stack<TreeNode>();
        TreeNode nowNode = root;
        
        while (!nodeStack.isEmpty() || nowNode != null) {
            if (nowNode != null) {
                result.add(nowNode.val);
                nodeStack.push(nowNode.right);
                nodeStack.push(nowNode.left);                
            }
            nowNode = nodeStack.pop();
        }
        return result;
    }
}

 

[LeetCode] Binary Tree Preorder Traversal

标签:des   style   blog   color   io   ar   div   sp   log   

原文地址:http://www.cnblogs.com/yuhaos/p/3958742.html

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