LeetCode OJ 94. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes‘ values.

For example:
Given binary tree [1,null,2,3],

   1
         2
    /
   3

return [1,3,2].

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

Subscribe to see which companies asked this question

解答

原来那个returnSize是拿来返回产生的中序遍历的数组大小用的……数组下标从0开始计算……

/**
 * 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* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int stack[1000], top = -1, i = 0;
    int *return_array = (int*)malloc(sizeof(int) * 1000);

    while(-1 != top||NULL != root){
        while(NULL != root){
            stack[++top] = root;
            root = root->left;
        }
        root = stack[top--];
        return_array[i++] = root->val;
        root = root->right;
    }
    *returnSize = i;
    return return_array;
}
时间: 2024-08-08 13:58:08

LeetCode OJ 94. Binary Tree Inorder Traversal的相关文章

【LeetCode】94. Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? OJ's Binary Tree Serialization: The seriali

LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively?中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法: 递归: 1 /**

94. Binary Tree Inorder Traversal 做题报告

题目链接: 94. Binary Tree Inorder Traversal 题目大意: 二叉树的中序遍历 做题报告: (1)该题涉及的算法,数据结构以及相关知识点 递归 (2)自己的解答思路+代码+分析时间和空间复杂度 递归思路 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) {

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

LeetCode 94 Binary Tree Inorder Traversal (中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 题目链接:https://leetcode.com/problems/binary-t

leetcode 94 Binary Tree Inorder Traversal ----- java

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 求二叉树的中序遍历,要求不是用递归. 先用递归做一下,很简单. /** * Defini

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其中序遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [1, 3, 2] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursi

Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive(递归) solution is trivial, could you do it iteratively(迭代)? 思路: 解法一:用递归方法很简单, (1)如果root为空,则返回NU