Binary Tree Preorder Traversal Java版本

原创 2015年07月06日 16:57:45
二叉树先序遍历
思路:

     不使用递归前序遍历,可以借助栈的帮助实现。对于一个特定节点,前序遍历顺序为: 根,左,右。所以入栈顺序为 右,左,根。由于根节点的遍历和展开(研究其右节点和左  节点)是同时的,所以根节点出栈的同时即可加入遍历结果中,然后研究其右节点和左节点,如果不为空即可入栈。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
  public ArrayList<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> returnList = new ArrayList<Integer>();
 
        if(root == null)
            return returnList;
 
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
 
        while(!stack.empty()){
            TreeNode n = stack.pop();
            returnList.add(n.val);
 
            if(n.right != null){
                stack.push(n.right);
            }
            if(n.left != null){
                stack.push(n.left);
            }
 
        }
        return returnList;
    }
}


PS快速入门视频教程【Java版本】

-
  • 1970年01月01日 08:00

LeetCode 144 — Binary Tree Preorder Traversal(C++ Java Python)

题目:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Given a binary tree, return the p...
  • dragon_dream
  • dragon_dream
  • 2014-03-02 22:13:14
  • 2594

Binary Tree Preorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/  跟Binary Tree Inorder Travers...
  • linhuanmars
  • linhuanmars
  • 2014-03-18 03:33:27
  • 13103

【LeetCode】144 . Binary Tree Preorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-22 23:53:41
  • 5611

LeetCode(144)Binary Tree Preorder Traversal

题目如下: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bi...
  • feliciafay
  • feliciafay
  • 2014-01-15 06:29:16
  • 1766

LeetCode144 Binary Tree Preorder Traversal(迭代实现) Java

题目: Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given bin...
  • l294265421
  • l294265421
  • 2016-04-23 13:14:19
  • 1605

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-06-01 23:42:08
  • 6375

LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

翻译给定一个二叉树,返回其前序遍历的节点的值。例如: 给定二叉树为 {1,#, 2, 3} 1 \ 2 / 3 返回 [1, 2, 3]备注:用递归是微不足道的,...
  • NoMasp
  • NoMasp
  • 2016-03-19 16:15:45
  • 3377

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的...
  • sunao2002002
  • sunao2002002
  • 2015-05-20 23:09:41
  • 1131

【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】

【106-Construct Binary Tree from Preorder and Inorder Traversal(通过前序和中序遍历构造二叉树)】【LeetCode-面试算法经典-Java...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-09 06:37:17
  • 2627
收藏助手
不良信息举报
您举报文章:Binary Tree Preorder Traversal Java版本
举报原因:
原因补充:

(最多只允许输入30个字)