当前位置:网站首页>145. Post order traversal of binary tree
145. Post order traversal of binary tree
2022-07-03 12:12:00 【zwanying】
The traversal of the tree :
1、 breadth-first level Use the iterative method .
2、 Depth first Before the order , Middle preface , In the following order , Refers to the order of root nodes use recursive , Iterative implementation .
Three elements of recursion :
1、 Parameters , Return value
2、 Termination conditions
3、 Every recursive logic
Recursive implementation
Before the order , Middle preface , Post sequence similar , Only the subsequent implementation is shown here .
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList();
digui(root,list);
return list;
}
public void digui(TreeNode r ,List<Integer> list){
if(r == null) return ;
// Right and left
digui(r.left,list);
digui(r.right,list);
list.add(r.val);
}
}
Iterative method
iteration , Implementation with stack .
The element to be output is out of the stack , Iterated element stack .
Iterative implementation
Preamble implementation
Press the right node first , Press the left node again , Make sure the output sequence is left first and then right .
Pay attention : Null pointers do not need to be stacked .
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
// Iterative method
if(root == null) return list;
Stack<TreeNode> stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode t = stack.pop();
list.add(t.val);
if(t.right != null) stack.push(t.right);
if(t.left != null) stack.push(t.left);
}
return list;
}
}
tip :
Note that the boundary value is empty .
In order to achieve
First traverse the left node , Pressing stack .
There is no left node , Out of the stack , Output .
Then traverse the right node , Pressing stack .
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
// In order to arrange Left in Right
List<Integer> list = new ArrayList<>();
TreeNode cur = root;
Stack<TreeNode> stack = new Stack<>();
if(root == null) return list;
while(!stack.isEmpty() || cur!=null){
if(cur!=null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
Subsequent implementation
Before the order Around the middle In the following order Right and left
To achieve Center right left , And then reverse , Achieve left and right .
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
// in , Right , Left
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root == null) return list;
stack.push(root);
while(!stack.isEmpty()){
TreeNode r = stack.pop();
list.add(r.val);
if(r.left != null) stack.push(r.left);
if(r.right != null) stack.push(r.right);
}
Collections.reverse(list);
return list;
}
}
Implementation of unified iterative method
The reasons for the inconsistency in the previous text :
Order of access , It is inconsistent with the order of traversing the returned results .
solve : If it is an element to traverse the result , The stack is followed by a null pointer to identify .
Preorder traversal is implemented as follows , In the sequence traversal , After the sequence traversal , Just fine tune the order .
The former sequence traversal : Around the middle . Stack order : Right , Left , in ( Processing elements , Followed by null identification )
In the sequence traversal : Left middle right . Stack order : Right , in ( Heel null identification ), Left
After the sequence traversal : Right and left . Deposit sequence : in ( Heel null identification ), Right , Left
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode t = stack.peek();
if(t == null){
stack.pop();
list.add(stack.pop().val);
}else{
stack.pop();
if(t.right != null) stack.push(t.right);
if(t.left != null) stack.push(t.left);
stack.push(t);
stack.push(null);
}
}
return list;
}
}
Pay attention to mistakes :
1、 The method to get the top element of the stack :peek()
2、 Remember to traverse every node , First out of the stack , Go back to the stack . Don't just get in and out , Operation exceeded expectations .
边栏推荐
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- CGroup introduction
- QT OpenGL rotate, pan, zoom
- [MySQL special] read lock and write lock
- php 获取文件夹下面的文件列表和文件夹列表
- Ripper of vulnhub
- 023(【模板】最小生成树)(最小生成树)
- Wechat applet development - page Jump transfer parameters
- Redis 笔记 01:入门篇
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
猜你喜欢
随机推荐
ES6新特性
PHP导出word方法(一mht)
C language improvement article (wchar_t) character type
Summary of development issues
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
temp
vulnhub之Ripper
vulnhub之tomato(西红柿)
023(【模板】最小生成树)(最小生成树)
Master and backup role election strategy in kept
MySQL searches and sorts out common methods according to time
Introduction to the implementation principle of rxjs observable filter operator
vulnhub之GeminiInc v2
Dart: About zone
Pragma pack syntax and usage
Jsup crawls Baidu Encyclopedia
安裝electron失敗的解决辦法
Go language to realize static server
【mysql专项】读锁和写锁