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

题目:

Given a binary tree, return the preorder traversal of its nodes’ values.

For example:

Given binary tree {1,#,2,3},

   1
         2
    /
   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

分析:

先根遍历二叉树,就是说,对二叉树中的每一个节点,先访问该节点,再访问其左子树,最后访问其右子树。用迭代的方式先根遍历二叉树,需要借助栈。具体步骤如下:

(1)将根结点入栈

(2)进入循环。先弹出栈顶元素,访问它,然后将该元素的右子树入栈,最后将该元素的左子树入栈。左子树后于右子树入栈保证了左子树先于右子树被访问。

代码:

package global;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

    @Override
    public String toString() {
        return "TreeNode [val=" + val + ", left=" + left + ", right=" + right
                + "]";
    }

}
package leetcode144;

import global.TreeNode;

import java.util.LinkedList;
import java.util.List;
/**
 * 先根遍历树的规则是这样的:对于遇到的每一个节点,先访问节点本身,然后是左子树根节点。
 * @author liyuncong
 *
 */
public class LeetCode144 {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<Integer>();
        if (root == null) {
            return result;
        }
        LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode top = stack.pop();
            if (top != null) {
                result.add(top.val);
                stack.push(top.right);
                stack.push(top.left);
            }
        }
        return result;
    }
}
时间: 2024-10-06 20:54:59

LeetCode144 Binary Tree Preorder Traversal(迭代实现) Java的相关文章

LeetCode144:Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 先序遍历应该是三种遍历方式中最好使用非递归来实现的了. 可以每次访问一个节点后再将其右子节点入栈,然后左子

Leet-code144. Binary Tree Preorder Traversal

这是一道将二叉树先序遍历,题目不难,采用深搜 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public static List<Integer> resultlist = new ArrayL

Binary Tree Preorder Traversal leetcode java

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 递归做法如下: 1     public void helper(Tree

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

LeetCode: Binary Tree Preorder Traversal 解题报告

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

40: Binary Tree Preorder Traversal

/************************************************************************/            /*       40:  Binary Tree Preorder Traversal                               */            /************************************************************************/ 

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

[Leetcode][Tree][Binary Tree Preorder Traversal]

二叉树的前序遍历:root点先被访问,然后是left和right子节点.迭代的版本也相对好写. 1.递归版本:时间复杂度O(N),空间复杂度O(N) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcod