首页 > 其他 > 详细

[LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

时间:2018-01-26 10:23:36      阅读:232      评论:0      收藏:0      [点我收藏+]

关于二叉树的遍历请看:

http://www.cnblogs.com/stAr-1/p/7058262.html

/*
    考察基本功的一道题,迭代实现二叉树前序遍历
     */
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root==null)
            return res;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty())
        {
            TreeNode cur = stack.pop();
            res.add(cur.val);
            if (cur.right!=null)
                stack.push(cur.right);
            if (cur.left!=null)
                stack.push(cur.left);
        }
        return res;
    }

 

[LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

原文:https://www.cnblogs.com/stAr-1/p/8358089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!