【数据结构】树前序遍历、中序遍历、后序遍历的递归与非递归方法java

1.递归方法

package test;

import java.util.Stack;

class Node {
	int val;
	Node left = null;
	Node right = null;
	Node(int val) {
		this.val = val;
	}
}
public class Tree {
	// 递归先序
	public void preRootTraversalR(Node root) {
		if (root == null) {
			return;
		}
		System.out.println(root.val);
		preRootTraversalR(root.left);
		preRootTraversalR(root.right);
	}


	public static void main(String[] args) {
		//      1
		//     2  3
		//    4 5 6 7
		//       8
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		Node node5 = new Node(5);
		Node node6 = new Node(6);
		Node node7 = new Node(7);
		Node node8 = new Node(8);
		node1.left = node2;
		node1.right = node3;
		node2.left = node4;
		node2.right = node5;
		node3.left = node6;
		node3.right = node7;
		node6.right = node8;
		Tree tree = new Tree();
		tree.after(node1);
	}
}

2.非递归先序

方法一:

	public void preRootTraversal(Node root) {
		if (root == null) {
			return;
		}
		Stack stack = new Stack<>();
		Node temp = root;
		while (temp != null || !stack.empty()) {
			while (temp != null) {
				System.out.println(temp.val);
				stack.add(temp);
				temp = temp.left;
			}
			temp = stack.pop().right;
		}
	}

方法二:

	public void query(Node root){//非递归先序
		if (root==null) return;
		Stack<Node> stack=new Stack<>();
		Node temp =root;
		stack.push(temp);
		while(!stack.empty()){
			temp=stack.pop();
			System.out.println(temp.val);
			if (temp.right!=null) 
				stack.push(temp.right);	
			if (temp.left!=null) 
				stack.push(temp.left);
		}
	}

3.非递归中序

方法一:


	public void mid(Node root) {
		if (root == null) {
			return;
		}
		Stack stack = new Stack<>();
		Node temp = root;
		while (temp != null || !stack.empty()) {
			while (temp != null) {
				stack.push(temp);
				temp = temp.left;
			}
			Node peek = stack.pop();
			System.out.println(peek.val);
			temp = peek.right;
		}
	}

方法二:

	public void query(Node root){//非递归中序
		if (root==null) return;
		Stack<Node> stack=new Stack<>();
		Node temp =root;
		while(!stack.empty()||temp!=null){
			if(temp!=null){
				stack.push(temp);
				temp=temp.left;
			}else{
				System.out.println(stack.peek().val);
				temp=stack.pop().right;
			}
		}
	}

4.非递归后序

public void query(Node root){//后序  左 右 根
		Stack<Node> stack=new Stack<>();
		Stack<Node> ret=new Stack<>();//存储最终的结果
		Node temp =root;
		stack.push(temp);
		while(!stack.empty()){
			temp=stack.pop();
			ret.push(temp);
			if (temp.left!=null) {
				stack.push(temp.left);
			}
			if (temp.right!=null) {
				stack.push(temp.right);
			}
		}
		while(!ret.empty()){
			temp=ret.pop();
			System.out.println(temp.val);
		}

	}

5.层序遍历

使用队列

public void query(Node root){//层序遍历
		if (root==null) return;
		Queue<Node> queue=new LinkedList<Node>();
		queue.add(root);
		while(!queue.isEmpty()){
			int len=queue.size();
			while(len>=0){
				Node node=queue.poll();
				System.out.println(node.val);
				if (node.left!=null) 
					queue.add(node.left);
				if (node.right!=null) 
				len--;
			}
		}
	}

6.路径总和

112.路径总和

import java.util.*;
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null) return false;
        sum-=root.val;
        if(root.left==null&&root.right==null){
            return sum==0;
        }
        return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
    }
}

113.路径总和II

import java.util.*;
class Solution {
    List<List<Integer>> list =new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if(root==null) return list;
        ArrayList<Integer> stack=new ArrayList<>();
        core(root,sum,stack);
        return list;
    }
    public void core(TreeNode root, int sum, ArrayList<Integer> stack) {
        if(root==null) return;
        if(sum==root.val&&root.left==null&&root.right==null){
            ArrayList<Integer> arr=new ArrayList<>();
            for(int i:stack){
                arr.add(i);
            }
            arr.add(root.val);
            list.add(arr);
        }
        stack.add(root.val);
        core(root.left,sum-root.val,stack);
        core(root.right,sum-root.val,stack);
        stack.remove(stack.size()-1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值