Leetcode--145. 二叉树的后序遍历(迭代&&递归)
发布日期:2021-04-30 21:04:34 浏览次数:92 分类:精选文章

本文共 1452 字,大约阅读时间需要 4 分钟。

???????

????

???????????????????

??

???[1, null, 2, 3]

???[3, 2, 1]


????

???????????????????????????????????

  • ??????????????
  • ???????????????????????
  • ??????????????????????????????????
  • ????

    import java.util.Stack;import java.util.List;import java.util.LinkedList;class Solution {    List
    postorderTraversal(TreeNode root) { List
    result = new LinkedList<>(); Stack
    stack = new Stack<>(); if (root == null) { return result; } stack.push(root); while (!stack.isEmpty()) { TreeNode temp = stack.pop(); result.add(temp.val); if (temp.left != null) { stack.push(temp.left); } if (temp.right != null) { stack.push(temp.right); } } return result; }}

    ????

    ???????????????????????????????????????????????????????????????

    ????

    import java.util.List;import java.util LinkedList;class Solution {    List
    result = new LinkedList<>(); public List
    postorderTraversal(TreeNode root) { if (root == null) { return result; } helper(root); return result; } private void helper(TreeNode root) { if (root == null) { return; } helper(root.left); helper(root.right); result.add(root.val); }}

    ??

    ??????????????????????????????????????????????????????

    上一篇:【剑指offer】面试题65:不用加减乘除做加法(Java)
    下一篇:javaIO流应用——通过【字节流】拷贝文件

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2026年06月16日 01时35分48秒