Leetcode 之Binary Tree Postorder Traversal(44)
后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> result;
stack<TreeNode *>s;
TreeNode *p, *q;//一个表示当前访问的结点,一个表示刚刚访问过的结点
p = root;
do
{
while (p != nullptr)
{
//不断将左结点压入
s.push(p);
p = p->left;
}
q = nullptr;//压到底时,刚刚访问过的结点必定为空结点
while (!s.empty())
{
p = s.top();
s.pop();
if (p->right == q)
{
//如果当前结点的右结点已经被访问,则访问该结点
result.push_back(p->val);
q = p;
}
else
{
//右孩子没有被访问过,则继续压入,先处理右子树
s.push(p);
p = p->right;
break;
}
}
} while (!s.empty());
}
Leetcode 之Binary Tree Postorder Traversal(44)的更多相关文章
- (二叉树 递归) 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 ...
随机推荐
- 洛谷 P1653 猴子 解题报告
P1653 猴子 题目描述 有N只猴子,第一只尾巴挂在树上,剩下的N-1只,要么被其他的猴子抓住,要么抓住了其他的猴子,要么两者均有.当然一只猴子最多抓两只另外的猴子.现在给出这N只猴子抓与被抓的信息 ...
- 全面解析JavaScript的Backbone.js框架中的Router路由
这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...
- JS判断当前DOM树是否加载完毕
/** * @function Monitor whether the document tree is loaded. * @param fn */function domReady(fn) { i ...
- jquery实现拖拽进度条并显示百分比
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- JavaScript中Unicode值转字符
在JavaScript中,将Unicode值转字符的方法: <!DOCTYPE html> <html> <head> <meta charset=" ...
- [codeforces/edu31]总结
链接:http://codeforces.com/contest/884 A题: 求86400-ai的前缀和,找到第一个大于等于给定t的即可. B题: 任意相邻两段之间必然有一个0,然后算一下至少的长 ...
- [存一下]iptables模块
介绍地址: http://www.tummy.com/blogs/2005/07/17/some-iptables-modules-you-probably-dont-know-about/ [1] ...
- HDU3524 数论
Perfect Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因
WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterPrope ...
- Http字段含义
转载自:http://blog.csdn.net/sand_ant/article/details/10503579 一.request请求Header简介 Accept:--客户机支持的类型 Acc ...