首页 > 其他 > 详细

Binary Tree Preorder Traversal

时间:2015-06-13 07:32:28      阅读:270      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!