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

Binary Tree Preorder Traversal

时间:2015-06-13 07:35:26      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

树的遍历

当初觉得难死,现在还好

public class Solution {
    public ArrayList<Integer>  preorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root==null) return res;
        Stack<TreeNode> st = new Stack<TreeNode>();
        st.add(root);
        while(!st.isEmpty()){
            TreeNode n = st.pop();
            res.add(n.val);
            if(n.right!=null)
                st.add(n.right);
            if(n.left!=null)
                st.add(n.left);
        }
        return res;
    }
}

 

Binary Tree Preorder Traversal

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4572920.html

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