binary-tree-inorder-traversal

ゞ 浴缸里的玫瑰 2021-11-09 08:36 202阅读 0赞

/**
*
* @author gentleKay
* Given a binary tree, return the inorder traversal of its nodes’ values.
* For example:
* Given binary tree{1,#,2,3},
1
\
2
/
3
* return[1,3,2].
* Note: Recursive solution is trivial, could you do it iteratively?
* confused what”{1,#,2,3}“means? > read more on how binary tree is serialized on OJ.
* 给定二叉树,返回其节点值的中序遍历。
* 例如:
* 给定二叉树1,,2,3,
* 返回[1,3,2]。
* 注意:递归解决方案很简单,可以迭代吗?
*/

推荐一个博客(关于递归和非递归二叉树的遍历)

https://blog.csdn.net/wang454592297/article/details/79472938

方法一:(非递归进行中序遍历)

  1. import java.util.ArrayList;
  2. import java.util.Stack;
  3. /**
  4. *
  5. * @author gentleKay
  6. * Given a binary tree, return the inorder traversal of its nodes' values.
  7. * For example:
  8. * Given binary tree{1,#,2,3},
  9. 1
  10. \
  11. 2
  12. /
  13. 3
  14. * return[1,3,2].
  15. * Note: Recursive solution is trivial, could you do it iteratively?
  16. * confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
  17. * 给定二叉树,返回其节点值的中序遍历。
  18. * 例如:
  19. * 给定二叉树1,,2,3,
  20. * 返回[1,3,2]。
  21. * 注意:递归解决方案很简单,可以迭代吗?
  22. */
  23. public class Main21 {
  24. public static void main(String[] args) {
  25. // TODO Auto-generated method stub
  26. // TreeNode root = null;
  27. TreeNode root = new TreeNode(4);
  28. root.left = new TreeNode(2);
  29. root.left.left = new TreeNode(1);
  30. root.left.right = new TreeNode(3);
  31. root.right = new TreeNode(6);
  32. root.right.left = new TreeNode(5);
  33. root.right.right = new TreeNode(7);
  34. root.right.right.right = new TreeNode(8);
  35. System.out.println(Main21.inorderTraversal(root));
  36. }
  37. public static class TreeNode {
  38. int val;
  39. TreeNode left;
  40. TreeNode right;
  41. TreeNode(int x) { val = x; }
  42. }
  43. public static ArrayList<Integer> inorderTraversal(TreeNode root) {
  44. Stack<TreeNode> stack = new Stack<>();
  45. ArrayList<Integer> array = new ArrayList<>();
  46. while (root != null || !stack.isEmpty()) {
  47. while (root != null) {
  48. stack.push(root);
  49. root = root.left;
  50. }
  51. if (!stack.isEmpty()) {
  52. root = stack.pop();
  53. array.add(root.val);
  54. root = root.right;
  55. }
  56. }
  57. return array;
  58. }
  59. }

方法二:(递归进行中序遍历)

  1. import java.util.ArrayList;
  2. import java.util.Stack;
  3. /**
  4. *
  5. * @author gentleKay
  6. * Given a binary tree, return the inorder traversal of its nodes' values.
  7. * For example:
  8. * Given binary tree{1,#,2,3},
  9. 1
  10. \
  11. 2
  12. /
  13. 3
  14. * return[1,3,2].
  15. * Note: Recursive solution is trivial, could you do it iteratively?
  16. * confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
  17. * 给定二叉树,返回其节点值的中序遍历。
  18. * 例如:
  19. * 给定二叉树1,,2,3,
  20. * 返回[1,3,2]。
  21. * 注意:递归解决方案很简单,可以迭代吗?
  22. */
  23. public class Main21 {
  24. public static void main(String[] args) {
  25. // TODO Auto-generated method stub
  26. // TreeNode root = null;
  27. TreeNode root = new TreeNode(4);
  28. root.left = new TreeNode(2);
  29. root.left.left = new TreeNode(1);
  30. root.left.right = new TreeNode(3);
  31. root.right = new TreeNode(6);
  32. root.right.left = new TreeNode(5);
  33. root.right.right = new TreeNode(7);
  34. root.right.right.right = new TreeNode(8);
  35. System.out.println(Main21.inorderTraversal(root));
  36. }
  37. public static class TreeNode {
  38. int val;
  39. TreeNode left;
  40. TreeNode right;
  41. TreeNode(int x) { val = x; }
  42. }
  43. static ArrayList<Integer> array = new ArrayList<>();
  44. public static ArrayList<Integer> inorderTraversal(TreeNode root) {
  45. if (root == null) {
  46. return array;
  47. }
  48. ergodic(root);
  49. return array;
  50. }
  51. public static void ergodic(TreeNode root) {
  52. if (root.left != null) {
  53. ergodic(root.left);
  54. }
  55. array.add(root.val);
  56. if (root.right != null) {
  57. ergodic(root.right);
  58. }
  59. }
  60. }

  

转载于:https://www.cnblogs.com/strive-19970713/p/11271596.html

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读