1、二叉树的前序遍历
2、N叉树的前序遍历
3、二叉树的中序遍历
4、二叉树的后序遍历
5、N叉树的后序遍历
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
/**
* 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<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
preOrder(root,result);
return result;
}
public void preOrder(TreeNode root,List<Integer> list){
if(root==null){
return ;
}
list.add(root.val);
preOrder(root.left,list);
preOrder(root.right,list);
}
}
图解:(来源:代码随想录)
link
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return result;
}
}
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if(root!=null){
stack.push(root);
}
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
stack.pop();
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
stack.push(node);
stack.push(null);
}else{
stack.pop();
node = stack.peek(); //取出栈中元素
stack.pop();
result.add(node.val);
}
}
return result;
}
}
给定一个 n 叉树的根节点 root ,返回 其节点值的 前序遍历 。
n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。
class Solution {
public List<Integer> preorder(Node root) {
//非递归
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
Node node = stack.pop();
res.add(node.val);
for(int i=node.children.size()-1;i>=0;i--){
stack.push(node.children.get(i));
}
}
return res;
}
}
class Solution {
public List<Integer> preorder(Node root) {
//递归
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
PreOrder2(root,res);
return res;
}
public void PreOrder2(Node root,List<Integer> list){
if(root==null) return ;
list.add(root.val);
for(int i=0;i<root.children.size();i++){
PreOrder2(root.children.get(i),list);
}
}
}
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
inorder(root,result);
return result;
}
public void inorder(TreeNode root,List<Integer> list){
if(root==null){
return ;
}
inorder(root.left,list);
list.add(root.val);
inorder(root.right,list);
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur!=null || !stack.isEmpty()){
if(cur!=null){
stack.push(cur);
cur=cur.left;
}else{
cur = stack.pop();
result.add(cur.val);
cur=cur.right;
}
}
return result;
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if(root!=null){
stack.push(root);
}
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
stack.pop();
if(node.right!=null){
stack.push(node.right);
}
stack.push(node);
stack.push(null);
if(node.left!=null){
stack.push(node.left);
}
}else{
stack.pop();
node = stack.peek();
stack.pop();
result.add(node.val);
}
}
return result;
}
}
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
postOrder(root,result);
return result;
}
public void postOrder(TreeNode root,List<Integer> list){
if(root==null){
return ;
}
postOrder(root.left,list);
postOrder(root.right,list);
list.add(root.val);
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
//搭了先序遍历的顺风车 根左右的顺序 根右左---> 翻转 后序(左右根)
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if(node.left!=null){
stack.push(node.left);
}
if(node.right!=null){
stack.push(node.right);
}
}
Collections.reverse(result);
return result;
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if(root!=null){
stack.push(root);
}
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
stack.pop();
stack.push(node);
stack.push(null);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}else{
stack.pop();
node = stack.peek();
stack.pop();
result.add(node.val);
}
}
return result;
}
}
给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。
n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。
class Solution {
public List<Integer> postorder(Node root) {
//非递归遍历
List<Integer> res = new ArrayList<>();
if(root==null) return res;
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
Node node = stack.pop();
res.add(node.val);
for(int i=0;i<node.children.size();i++){
stack.push(node.children.get(i));
}
}
Collections.reverse(res);
return res;
}
}
class Solution {
public List<Integer> postorder(Node root) {
//递归遍历
List<Integer> res = new ArrayList<>();
if(root==null) return res;
postorder2(root,res);
return res;
}
public void postorder2(Node root,List<Integer> list){
if(root==null) return ;
for(int i=0;i<root.children.size();i++){
postorder2(root.children.get(i),list);
}
list.add(root.val);
}
}
1、确定递归函数的参数返回值
2、确定终止条件
3、确定单层递归的逻辑