LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal

My Submissions

Question

Total Accepted: 108336 Total
Submissions: 278322 Difficulty: Medium

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?

Subscribe to see which companies asked this question

Show Tags

Show Similar Problems

Have you met this question in a real interview?

Yes

No

Discuss

求一棵二叉树的先根遍历序。题目说了,递归的解法十分简单,能否用非递归的方式求解。

每遍历一个节点的时候,将右孩子入栈。向左搜索直到为空时,弹栈。

我的AC代码

public class BinaryTreePreorderTraversal {

	public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
			while (cur != null) {
				list.add(cur.val);
				if(cur.right != null) stack.add(cur.right);
				cur = cur.left;
			}
			if(!stack.isEmpty()) cur = stack.pop();
		}
        return list;
    }
}
时间: 2024-10-21 00:58:19

LeetCode 144. Binary Tree Preorder Traversal 解题报告的相关文章

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?

Java for LeetCode 144 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]. 二叉树的前序遍历,根节点→左子树→右子树 解题思路一: 递归实现,JAVA实现如下: public List<Integer> preorderTraversal(TreeNode root) { List<Int

Java [Leetcode 144]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]. 解题思路: 这个问题最简单的方法是使用递归,但是题目规定不能使用,得使用迭代的方法. 那么我们考虑使用栈来实现. 思路是每次遍历这节点,把该点的值放入list中,然后把该点的右孩子放入栈中,并将当前点设置为左孩子

LeetCode 144 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://leetcode.com/problems/binary-tre

leetcode 144. 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? 求前序遍历,要求不用递归. 使用双向队列. /** * Definition for a b

leetcode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

前序遍历的递归解法: 方法一C++: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<

LeetCode 589 N-ary Tree Preorder Traversal 解题报告

题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回结点值的前序遍历.可以使用递归的方法做.因为是前序遍历,所以最开始就加入根结点的值. python代码 """ # Definition for a Node. class Node: def __init__(self, val, children): self.val = v

LeetCode 144. Binary Tree Preorder Traversal

前序遍历 递归: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> res;

LeetCode 144. Binary Tree Preorder Traversal 动态演示

先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> ret; if(!root) return ret; stack<TreeNode*> stk; stk.push(root); //ahd(root) //a(stk) //a(ret) while(stk.size()>0){ TreeNode*