当前位置:网站首页>【leetcode】145. Binary Tree Postorder Traversal
【leetcode】145. Binary Tree Postorder Traversal
2022-04-22 10:29:00 【Front end corner】
subject
Give you the root node of a binary tree
root, Returns the After the sequence traversal .
Example 1:
Input :root = [1,null,2,3]
Output :[3,2,1]
Example 2:
Input :root = []
Output :[]
Example 3:
Input :root = [1]
Output :[1]
Tips :
The number of nodes in the tree is in the range [0, 100] Inside
-100 <= Node.val <= 100
Ideas

- Recursive writing
- The function parameters are the current binary tree node and an array used to store the results
- The termination condition is that the node is empty
- The order of traversal after binary tree is “ Right and left ”, Therefore, single-layer recursive logic is to recurse the left node first , Recursive right node , Finally, the value of the intermediate node is stored in the result array .
- Iterative writing
- Iterative writing can first look at the iterative writing of preorder traversal 【leetcode】144. Preorder traversal of two tree , The order of precedence traversal is “ Around the middle ”, The order of subsequent traversal is “ Right and left ”, Exchange the traversal order of the left node and the right node in the preorder traversal , become " Center right left ", Then invert the result array to get “ Right and left ”.
- The code only needs to be changed slightly based on the previous traversal .
Code

- recursive
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number[]} */
var postorderTraversal = function(root) {
function traversal(node , ans){
if(!node) return
// Recursive left node
traversal(node.left , ans)
// Recursive right node
traversal(node.right , ans)
// Store the value of the intermediate node into the result array
ans.push(node.val)
}
let ans = []
traversal(root , ans)
return ans
};
- iteration
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number[]} */
var postorderTraversal = function(root) {
if(!root) return []
let stack = []
let ans = []
queue.push(root)
while(stack.length){
const node = stack.pop()
// The intermediate node value is added to the result array
ans.push(node.val)
// Left node first stack
if(node.left) stack.push(node.left)
// Right node back into the stack
if(node.right) stack.push(node.right)
}
// After the result array is flipped, it returns
return ans.reverse()
};
Pay attention to my special column , Update three times a day leetcode Answer key , Get stronger together !
版权声明
本文为[Front end corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221027269836.html
边栏推荐
- 【uvm】 raise_ Statements that consume simulation time cannot be added before object
- 「译文」给讨厌YAML的人的10个写YAML的建议
- 基于SSM的电影在线购票系统
- Spa first screen loading optimization
- LLVM之父Chris Lattner:编译器的黄金时代
- [SV] assign force difference
- MySQL进阶之数据的增删改查(DML)
- 【设计必用!】网页设计常用色彩搭配表 《配色表》
- 多语言通信基础 05 go的grpc体验
- 《MySQL 是怎样运行的:从根儿上理解 MySQL 》优质笔记
猜你喜欢
随机推荐
dnspy 修改 伊格利亚战记 军队维护费和英雄维护系数
matlab的解决反复激活问题的license.lic文件
How to implement acid at the bottom of MySQL?
Google Earth engine (GEE) -- aggregate grid population data
Symfony3.4 数据库反向生成entity 已解决
来文章啦~分享压缩和解压文件【在线网站】
數字化時代,企業運維面臨現狀及挑戰分析解讀
MySql5.7.26安装
golang 时间格式化
【设计必用!】网页设计常用色彩搭配表 《配色表》
【307期】从实现原理来讲,Nacos 为什么这么强?
.pm format perl
Some functions of qbytearray are used for conversion
项目如何解决跨域问题
PCIE XDMA IP核介绍(附列表)-明德扬科教(mdy-edu.com)
Google Adsense suggests that the advertising capture tool is wrong, which may lead to reduced revenue. What should we do
VMware virtual machine download and installation tutorial
谷歌开发者工具preserve log
TC397 EVADC
【sv】 assign force区别









