Leetcode 之Binary Tree Postorder Traversal(46)
采用广度优先遍历,一个变量记录层数,一个变量记录方向.
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)的更多相关文章
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for 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 ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- leetcode - [6]Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
随机推荐
- 洛谷 P3332 [ZJOI2013]K大数查询 解题报告
P3332 [ZJOI2013]K大数查询 题目描述 有\(N\)个位置,\(M\)个操作.操作有两种,每次操作如果是\(\tt{1\ a\ b\ c}\)的形式表示在第\(a\)个位置到第\(b\) ...
- 从零开始学Linux系统(二)之基本操作指令
ifconfigping ip地址帮助:ping -t ip地址ping -c 次数 ip地址ping -s 包的大小关机重启:shutdown -h now reboot清屏:clear == C ...
- swift的UIbutton
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...
- 微服务学习一:idea中springboot集成mybatis
一直都想学习微服务,这段时间在琢磨这块的内容,个人之前使用eclipse,现在用intellij idea来进行微服务的开发,个人感觉intellij idea比eclipse更简洁更方便,因为int ...
- linux shell脚本攻略笔记
前一阵子系统学习了下<linux shell脚本攻略>这本书.在此记录下自己的学习笔记 1. 输出颜色字符 echo -e "\e[1:41m" 1表示背景色 2 ...
- Generating Sets 贪心
H - Generating Sets Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- footer点击添加active class
//footer点击添加active class var indexFooter= document.querySelectorAll('#index_footer li'); for (var i ...
- js:鼠标移动到文字显示div,移出文字div显示,鼠标能移进div
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- 查看自己电脑外网IP
连着wifi,在CMD窗口中显示的无线局域网适配器IP很有可能是内网IP.此时可以通过下面方法查看自己的电脑外网IP. 1.前提条件可以上外网: 2.上外网百度,输入IP,进行搜索. 3.查看结果即可 ...
- 【Android】Android之Copy and Paste
Android为复制粘贴提供了一个强大的基于剪切板的框架,它支持简单和复杂的数据类型,包括纯文本,复杂的数据结构,二进制流,甚至app资源文件.简单的文本数据直接存储在剪切板中,而复杂的数据则存储的是 ...