-
中序遍历:左根右,从与主根邻近的左树开始
//递归 public static void inOrder(TreeNode root){ if(root != null){ inOrder(root.lchild); System.out.println(root.data + " "); inOrder(root.rchild); } }
//中序遍历 Stack<TreeNode> stack = new Stack<>(); List<TreeNode> list = new ArrayList<>(); public List<TreeNode> inOrder(TreeNode root){ while(root!=null || !stack.isEmpty()){ while(root!=null){ stack.push(root); root = root.left; } root = stack.pop(); list.add(root); root = root.right; } return list; }
二叉树--中序遍历
最新推荐文章于 2024-07-07 13:48:02 发布