LeetCode 二叉树反序遍历(binary-tree-postorder-traversal)

Given a binary tree, return the postorder traversal of its nodes‘ values.

For example:
Given binary tree{1,#,2,3},

   1
         2
    /
   3

return[3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

import java.util.*;
public class Solution {
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(root==null)
            return list;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        Stack<TreeNode> stack2 = new Stack<TreeNode>();
        stack.add(root);
        while(!stack.isEmpty()){
            TreeNode r = stack.pop();
            if(r.left!=null)
                stack.add(r.left);
            if(r.right!=null)
                stack.add(r.right);
            stack2.add(r);
        }
        while(!stack2.isEmpty()){
            list.add(stack2.pop().val);
        }
        //LRD(list,root);递归
        return list;
    }
    public void LRD(ArrayList<Integer> list,TreeNode root){
        if(root==null)
            return;
        LRD(list,root.left);
        LRD(list,root.right);
        list.add(root.val);
    }
}
时间: 2024-11-08 13:32:24

LeetCode 二叉树反序遍历(binary-tree-postorder-traversal)的相关文章

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

leetcode 刷题之路 78 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 非递归方式实现二叉树的后序遍历. 思路:使用栈辅助实现,首先将根节点入栈,然后以栈是否为

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

LeetCode: Binary Tree Postorder Traversal [145]

[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? [题意] 非递归实现后续遍历 [思路] 维护两个栈,一个栈用来存储标记,标记相

LeetCode——Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 中文:二叉树的后续遍历(左-右-根).能用非递归吗? 递归: public class

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

Binary Tree Postorder Traversal leetcode java

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 递归方法代码: 1     public void helper(Tre