标签:思路 lse 结果 arraylist 后序 数据结构 || problems cti
// 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 {
List<Integer> result =new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root == null)
return result;
if(root.left != null)
postorderTraversal(root.left);
if(root.right != null)
postorderTraversal(root.right);
result.add(root.val);
return result;
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result =new ArrayList<>();
if(root == null)
return result;
//pre的作用是记忆上一次遍历的节点,以便判断是否有右子树或右子树未遍历
TreeNode node = root,pre = null;
Stack<TreeNode> stack =new Stack<>();
while(!stack.isEmpty() || node != null){
while(node != null){
stack.push(node);
node = node.left;
}
if((node=stack.peek()).right == null
|| node.right == pre){
pre = node;
result.add(stack.pop().val);
node =null;
}else{
pre = node;
node =node.right;
}
}
return result;
}
}
//思路:
//前序遍历的过程是:(根-左-右)
//那么我们遍历的过程变为:(根-右-左),最后的结果再进行下倒置
//顺序就变成了:(左-右-根),完成对后序的遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result =new ArrayList<>();
if(root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode node =root;
while(!stack.isEmpty() || node != null){
while(node != null){
stack.push(node);
result.add(node.val);
node = node.right;
}
node = stack.pop().left;
}
Collections.reverse(result);
return result;
}
}
//还是借鉴前序遍历的思路,莫里斯遍历可参看我的中序遍历一文
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root == null){
return result;
}
TreeNode node =root,pre = null;
while(node != null){
if(node.right == null){
result.add(node.val);
node =node.left;
}else{
pre = node.right;
while(pre.left != null && pre.left != node){
pre = pre.left;
}
if(pre.left == null){
pre.left = node;
result.add(node.val);
node = node.right;
}else{
pre.left = null;
node =node.left;
}
}
}
Collections.reverse(result);
return result;
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> result =new LinkedList<>();
LinkedList<TreeNode> stack =new LinkedList<>();
if(root == null){
return result;
}
stack.addFirst(root);
while(!stack.isEmpty()){
TreeNode node =stack.removeFirst();
result.addFirst(node.val);
if(node.left != null){
stack.addFirst(node.left);
}
if(node.right != null){
stack.addFirst(node.right);
}
}
return result;
}
}
标签:思路 lse 结果 arraylist 后序 数据结构 || problems cti
原文地址:https://www.cnblogs.com/CodingXu-jie/p/13866761.html