LeetCode OJ 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?

Subscribe to see which companies asked this question

解答

注意空树的情况……

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int top = -1, i = 0;
    int *return_array = (int*)malloc(sizeof(int) * 1000);
    struct TreeNode *stack[1000];

    if(NULL == root){
        *returnSize = i;
        return return_array;
    }
    stack[++top] = root;
    while(-1 != top){
        root = stack[top--];
        return_array[i++] = root->val;
        if(NULL != root->right)
            stack[++top] = root->right;
        if(NULL != root->left)
            stack[++top] = root->left;
    }
    *returnSize = i;
    return return_array;
}
时间: 2024-11-08 23:49:20

LeetCode OJ 144. Binary Tree Preorder Traversal的相关文章

【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? 提示: 首先需要明确前序遍历的顺序,即:节点 -> 左孩子 -> 右孩子,这一顺序

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

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

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 ----- 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 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;

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios