From bd1e12ff7e0dac5a244aa51cc1024b84036af5ae Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 9 Apr 2021 14:49:17 +0800 Subject: [PATCH] =?UTF-8?q?145:=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/BinaryTreePostorderTraversal.java | 62 +++++++++++++++++++ .../editor/cn/BinaryTreePostorderTraversal.md | 15 +++++ 2 files changed, 77 insertions(+) create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.java create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.md diff --git a/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.java b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.java new file mode 100644 index 0000000..b18ba41 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.java @@ -0,0 +1,62 @@ +//给定一个二叉树,返回它的 后序 遍历。 +// +// 示例: +// +// 输入: [1,null,2,3] +// 1 +// \ +// 2 +// / +// 3 +// +//输出: [3,2,1] +// +// 进阶: 递归算法很简单,你可以通过迭代算法完成吗? +// Related Topics 栈 树 +// 👍 564 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +import java.util.ArrayList; +import java.util.List; + +//145:二叉树的后序遍历 +public class BinaryTreePostorderTraversal{ + public static void main(String[] args) { + //测试代码 + Solution solution = new BinaryTreePostorderTraversal().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * 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 postorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + if(root==null){ + return list; + } + list.addAll(postorderTraversal(root.left)); + list.addAll(postorderTraversal(root.right)); + list.add(root.val); + return list; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.md b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.md new file mode 100644 index 0000000..81746f9 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreePostorderTraversal.md @@ -0,0 +1,15 @@ +

给定一个二叉树,返回它的 后序 遍历。

+ +

示例:

+ +
输入: [1,null,2,3]  
+   1
+    \
+     2
+    /
+   3 
+
+输出: [3,2,1]
+ +

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

+
Related Topics
  • \n
  • 👍 564
  • 👎 0
  • \ No newline at end of file