采用广度优先遍历,一个变量记录层数,一个变量记录方向.

void traverse(TreeNode *root, vector<vector<int>> result, int level, bool left_to_right)
{
if (!root)return;
//如果进入下一层了,则result同样也加一层
if (level > result.size())result.push_back(vector<int>());
//如果此时为从左向右,则直接压入即可
if (left_to_right)
result[level - ].push_back(root->val);
//如果此时从右向左,则从头插入
else
result[level - ].insert(result[level - ].begin(), root->val); traverse(root->left, result, level + , !left_to_right);
traverse(root->right, result, level + , !left_to_right);
}
vector<vector<int>> zigzagLevelOrder(TreeNode *root)
{
//广度优先遍历
vector<vector<int>> result;
traverse(root, result, , true);
return result;
}

Leetcode 之Binary Tree Postorder Traversal(46)的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  4. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  6. leetcode 145. Binary Tree Postorder Traversal ----- java

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

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  8. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  9. leetcode - [6]Binary Tree Postorder Traversal

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

  10. 【leetcode】Binary Tree Postorder Traversal

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

随机推荐

  1. BZOJ3196 & 洛谷3380:二逼平衡树——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3196 https://www.luogu.org/problemnew/show/P3380 (题 ...

  2. [BZOJ1131/POI2008]Sta树的深度

    Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...

  3. [APIO2017]商旅

    link 这题卡我精度,调了一晚上才调对,因为没有想到图还可以不连通 其实可以预处理出好多东西,距离($dis(u,v)$),买卖物品(从$u$到$v$买卖物品的最大利润,例($max{S_{u,i} ...

  4. LoadRunner中的IP欺骗

    应用程序服务器和网络设备使用IP地址来识别客户端.应用程序服务器通常会对来自同一计算机的客户端信息进行高速缓存. 网络路由器尝试对原信息和目标信息进行高速缓存以优化吞吐量.如果多个用户具有相同的IP地 ...

  5. 第九章 C99可变长数组VLA详解

    C90及C++的数组对象定义是静态联编的,在编译期就必须给定对象的完整信息.但在程序设计过程中,我们常常遇到需要根据上下文环境来定义数组的情况,在运行期才能确知数组的长度.对于这种情况,C90及C++ ...

  6. ZooKeeper管理员指南——部署与管理ZooKeeper

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 系统环境 集群模式的配置 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生产环境的ZK集群. ...

  7. Tomcat7项目迁移到Tomcat8中文乱码问题

    我打算开始使用Tomcat8了,先解决中文乱码问题,在解决其它的问题! 个人推荐:修改server.xml方式 对于SpringMVC报的错误我稍后在补充问题 1.问题描述 Tomcat 7下项目切换 ...

  8. [Leetcode] LRU 算法实现

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  10. elasticsearch client

    你可以用client做很多事情: 在集群中执行index, get, delete, search,update 操作 在集群中执行administrative tasks 如果你想再程序中嵌入ela ...