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) {
    , i = ;
    );
    ];

    if(NULL == root){
        *returnSize = i;
        return return_array;
    }
    stack[++top] = root;
     != 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;
}

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

  1. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  3. 【LEETCODE OJ】Binary Tree Preorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...

  4. 【LeetCode】144. Binary Tree Preorder Traversal

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

  5. LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  8. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

随机推荐

  1. 00012 - ps命令详解

    使用权限:所有使用者使用方式:ps [options] [--help]说明:显示瞬间行程 (process) 的动态参数:ps的参数非常多, 在此仅列出几个常用的参数并大略介绍含义-A    列出所 ...

  2. Shell IF条件判断解析

    IF条件判断 1.基本语法: if [ command ]; then #符合该条件执行的语句 fi2.扩展语法: if [ command ];then #符合该条件执行的语句 elif [ com ...

  3. MySQL常用语句大全

    数据库操作:创建数据库create database database_name 查看数据库 show databases使用数据库use dbname删除数据库 drop database dbna ...

  4. Sep 10th 2018

    今天是教师节,祝家里的两位‘老师’节日快乐.一位是幼儿园的保健医,另一位是驾校的教练.不能说是真正的老师,但作的也是传道授业之工作.今天看到新闻,马云要在明年的今天辞去现任阿里巴巴主席一职,继续投身他 ...

  5. iproute2 对决 net-tools

    如今很多系统管理员依然通过组合使用诸如ifconfig.route.arp和netstat等命令行工具(统称为net-tools)来配置网络功能,解决网络故障.net-tools起源于BSD的TCP/ ...

  6. 备用DNS域名服务器

    DNS:1.34.151.129,域名:www#eliuliang#com, 个人用解析地址,请勿使用.

  7. django练习题

    1.Web框架的本质是什么?为什么要有Web框架? 所有的Web应用,本质上其实就是一个socket服务端,用户端程序其实就是一个socket客户端.对于真实开发中的python web程序来说,一般 ...

  8. css的优化规则

    1.避免过度约束: // 不推荐 ul.nav{..} // 推荐 .nav{..}  2.避免过长的后代选择符: // 不推荐 html div tr td {..} 3.避免链式(交集)选择符: ...

  9. linux设置服务器时间同步

    yum install -y rdate 服务器请设置 */5 * * * * /usr/bin/rdate -s time-b.nist.gov ubuntu 设定时区:dpkg-reconfigu ...

  10. element-ui 带单选框的表格

    效果:不只是带单选框,点击当前行单选框选中状态网上查了一些发现很多都是只能点击当前radio选中当前行,配合element-ui的单选table时发现两个的选择状态是不一致的,所以调整了一下效果 提供 ...