From cdb1ce829ebb5a50aa394a0640bff92f8ff493f5 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Thu, 1 Apr 2021 16:30:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A94:=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E4=B8=AD=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/BinaryTreeInorderTraversal.java | 105 ++++++++++++++++++ .../editor/cn/BinaryTreeInorderTraversal.md | 52 +++++++++ 2 files changed, 157 insertions(+) create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.java create mode 100644 LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.md diff --git a/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.java b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.java new file mode 100644 index 0000000..70dae12 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.java @@ -0,0 +1,105 @@ +//给定一个二叉树的根节点 root ,返回它的 中序 遍历。 +// +// +// +// 示例 1: +// +// +//输入:root = [1,null,2,3] +//输出:[1,3,2] +// +// +// 示例 2: +// +// +//输入:root = [] +//输出:[] +// +// +// 示例 3: +// +// +//输入:root = [1] +//输出:[1] +// +// +// 示例 4: +// +// +//输入:root = [1,2] +//输出:[2,1] +// +// +// 示例 5: +// +// +//输入:root = [1,null,2] +//输出:[1,2] +// +// +// +// +// 提示: +// +// +// 树中节点数目在范围 [0, 100] 内 +// -100 <= Node.val <= 100 +// +// +// +// +// 进阶: 递归算法很简单,你可以通过迭代算法完成吗? +// Related Topics 栈 树 哈希表 +// 👍 902 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +import java.util.ArrayList; +import java.util.List; + +//94:二叉树的中序遍历 +public class BinaryTreeInorderTraversal { + public static void main(String[] args) { + //测试代码 + Solution solution = new BinaryTreeInorderTraversal().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 inorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + inorder(root, list); + return list; + } + + private void inorder(TreeNode root, List list) { + if (root == null) { + return; + } + inorder(root.left, list); + list.add(root.val); + inorder(root.right, 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/BinaryTreeInorderTraversal.md b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.md new file mode 100644 index 0000000..0c05d3f --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeInorderTraversal.md @@ -0,0 +1,52 @@ +

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,null,2,3]
+输出:[1,3,2]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1]
+输出:[1]
+
+ +

示例 4:

+ +
+输入:root = [1,2]
+输出:[2,1]
+
+ +

示例 5:

+ +
+输入:root = [1,null,2]
+输出:[1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点数目在范围 [0, 100]
  • +
  • -100 <= Node.val <= 100
  • +
+ +

 

+ +

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

+
Related Topics
  • 哈希表
  • \n
  • 👍 902
  • 👎 0
  • \ No newline at end of file