LeetCode OJ 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [3,2,1].
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* postorderTraversal(struct TreeNode* root, int* returnSize) {
], top = -, i = ;
);
struct TreeNode *visit = NULL;
!= top||NULL != root){
while(NULL != root){
stack[++top] = root;
root = root->left;
}
root = stack[top];
if(NULL == root->right||visit == root->right){
return_array[i++] = root->val;
top--;
visit = root;
root = NULL;
}
else{
root = root->right;
}
}
*returnSize = i;
return return_array;
}
LeetCode OJ 145. Binary Tree Postorder Traversal的更多相关文章
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 二叉树前序、中序、后序非递归遍历 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] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- vue vue-resource 请求数据
main.js import Vue from 'vue'; import App from './App.vue'; /*使用vue-resource请求数据的步骤 1.需要安装vue-resour ...
- sas transpose 代码备忘
OPTIONS NOCENTER LS=MAX PS=MAX; LIBNAME S '.\report';/*PROC PRINT DATA=S.doquestionr(WHERE=(sid=1972 ...
- 第2章 GNS3和PacketTracer网络模拟器(3)_搭建Packet tracer实验环境
3. Packet tracer实验环境 3.1 设置网络拓扑图 (1)配置路由器局域网和广域网接口,如上图(可双击相应的图标,然后在命令行或图形界面上进行IP地址等配置) ①本例采用“Generic ...
- Windows 2016 无域故障转移群集部署方法 超详细图文教程 (二)
上一章我们配置了一台设备,接着根据那个配置,配置其它设备.这里我配置了三台设备: 创建故障转移群集,并添加设备. 之前的操作都是每台服务器都要做的,而这个操作,只需要任选一台去做即可,我这里选d1 1 ...
- JavaScript之函数,词法分析,内置对象和方法
函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别. // 普通函数定义 function f1() { console.log("Hello wo ...
- BZOJ2560串珠子
/* 很清新的一道题(相比上一道题) g[S]表示该 S集合中胡乱连的所有方案数, f[S] 表示S集合的答案 那么F[S] 等于G[S]减去不合法的部分方案 不合法的方案就枚举合法的部分就好了 g[ ...
- shell脚本选择LOG里面特定的行,生成新文件并rsync上传
rsync.sh #!/bin/bash tool_path=$(cd `dirname $`; pwd) eval `cat ${tool_path}/conf.properties` rsync_ ...
- day23面向对象编程基础
面向对象编程基础1.面向过程的编程思想 核心过程二字,过程指的是解决问题的步骤,即先干什么\再干什么\后干什么 基于该思想编写程序就好比在设计一条流水线,是一种机械式的思维方式 优点 ...
- jmeter本机内存溢出如何修改?
websocket连接过程中内存溢出,本机配置的内存最大和最小设置的512: 一.后台返回 二.结果树返回: Thread Name: 线程组 1-9Sample Start: 2017-09-11 ...
- alias with parameter,linux
alias demoAlias1='_(){ git checkout -b $1; command2;}; _'