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) {
, 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的更多相关文章
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
- 【LeetCode】144. Binary Tree Preorder Traversal
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
随机推荐
- 第10章 网络安全(4)_网络层安全IPSec
5. 网络层安全IPSec 5.1 IPSec协议 (1)前面使用Outlook进行数字签名和数字加密是应用层实现的安全.安全套接字实现的安全是在应用层和传输层之间插入了一层来实现数据通信安全.而IP ...
- Delphi2010如何获取程序内部自身版本号?
用原来的GetFileVersionInfo只能获取Delpi7的程序版本号,用在Delphi2010中就不管用了 //------ 获取文件版本号function F_GetFileVersion( ...
- (转)C# WebApi 跨域问题解决方案:CORS
原文地址:http://www.cnblogs.com/landeanfen/p/5177176.html 阅读目录 一.跨域问题的由来 二.跨域问题解决原理 三.跨域问题解决细节 1.场景描述 2. ...
- How Computers Boot Up.计算机的引导过程
原文标题:How Computers Boot Up 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译一下.一来自己复习 ...
- ob系列函数中常用函数
ob_start(); //打开一个输出缓冲区,所有的输出信息不再直接发送到浏览器,而是保存在输出缓冲区里面. ob_clean(); //删 ...
- [Unity插件]Lua行为树(一):BehaviorDesigner源码分析
BehaviorDesigner是Unity上的一款行为树插件,不过这个插件是用C#编写的,编写出来的行为树也是依赖于C#的,不利于热更,所以有必要写一个lua版本的. 首先下载BehaviorDes ...
- win10更改hosts文件
管理员身份运行notepad,打开hosts文件即可.
- javafx自动补全
public boolean showUpload2HomeDialog(final OperationInfo initDataInfo) { boolean result = false; try ...
- JAVA获取文件夹下所有的文件
package com.test; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; im ...
- GPUImage中曝光滤镜实现——GPUImageExposureFilter
核心代码: varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform highp float ...