Given a binary tree, return the postorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [3,2,1]

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

Solution:

  使用两个栈,来实现;

  使用一个栈来实现

  

 class Solution {
public:
ListNode* sortList(ListNode* head) {
if (head == nullptr || head->next == nullptr)return head;
ListNode *slow = head, *fast = head, *pre = head;
while (fast != nullptr && fast->next != nullptr)
{
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = nullptr;//将链表分为两段
return merge(sortList(head), sortList(slow));
}
ListNode* merge0(ListNode* l1, ListNode* l2)
{
if (l1 == nullptr || l2 == nullptr)return l1 == nullptr ? l2 : l1;
if (l1->val < l2->val)
{
l1->next = merge(l1->next, l2);
return l1;
}
else
{
l2->next = merge(l1, l2->next);
return l2;
}
}
ListNode* merge(ListNode* l1, ListNode* l2)
{
ListNode* ptr = new ListNode(-);
ListNode* p = ptr;
while (l1 != nullptr && l2 != nullptr)
{
if (l1->val < l2->val)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if (l1 == nullptr)p->next = l2;
if (l2 == nullptr)p->next = l1;
return ptr->next;
}
};

力扣算法题—145BinartTreePostorderTraversal的更多相关文章

  1. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  4. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  5. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  6. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  7. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  8. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  9. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

随机推荐

  1. javascript: 禁用右键、文本选择功能、复制按键

    <script type="text/javascript"> //禁用右键.文本选择功能.复制按键 //http://www.jinyuanbao.cn $(docu ...

  2. git使用记录九:开发中临时加塞了紧急任务怎么处理

    开发中临时加塞了紧急任务怎么处理 隐藏工作区域 git stash git status 查询隐藏的列表 git stash list 处理完bug,提交之后,再恢复隐藏的工作区域 git stash ...

  3. 转 : jconsole 和jvisualVM 监控远程 spring boot程序

    监控java 程序 增加启动参数 java  \ -Djava.rmi.server.hostname=192.168.2.39 \ -Dcom.sun.management.jmxremote \- ...

  4. if语句的嵌套使用之获取三个数据的最大值

    获取三个数据的最大值: class Hello2 { public static void main(String[] args) { int a = 10; int b = 20; int c = ...

  5. C++中的多重继承(二)

    1,本文分析另一个多重继承问题及其工程中的解决方案,单继承加多接口实现的开发方式: 2,多重继承的问题三: 1,多重继承可能产生多个虚函数表: 1,实际工程中可能造成不可思议的问题,并且这些问题很难以 ...

  6. TP框架实现文件的下载(主要解决文件存在中文文件名的问题)

    namespace Home\Controller; use Think\Controller; use Org\Net\Http; class IndexController extends Con ...

  7. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...

  8. Struts2后台使用Request和Session方法

    在Struts2后台,如果需要使用Request和Session的话,可以通过下面的方法: 主要是利用了com.opensymphony.xwork2.ActionContext类以及ora.apac ...

  9. react学习笔记_03-组件&props

    组件 & Props的学习 组件允许你将 UI 拆分为独立可复用的代码片段,并对每个片段进行独立构思. 组件,从概念上类似于 JavaScript 函数.它接受任意的入参(即 “props”) ...

  10. QT问题解决

    1.pro文件下各个变量的含义 https://www.zybuluo.com/breakerthb/note/582395 2.如何在pro文件中导入其他的库 https://blog.csdn.n ...