二叉树的中序遍历

//中序遍历
public class Solution{
	public List<Integer> inorder(TreeNode root){
		//递归
		List<Integer> res=new ArrayList<>();
		recur(root,res);
		return res;
	}	
	public void recur(TreeNode root,List<Integer> res){
		if(root==null) return;
		recur(root.left,res);
		res.add(rot.val,res);
		recur(root.right,res);
	}
}
public class Solution
{	
	//中序遍历
	public List<Integer> inorder(TreeNode root){
		List<Integer> res=new ArrayList<>();
		Stack<TreeNode> stack=new Stack<>();
		TreeNode cur=root;
		while(cur!=null||!stack.isEmpty()){
			while(cur!=null){
				stack.push(cur);
				cur=cur.left;
			}
			cur=stack.pop();
			list.add(cur.val);
			cur=cur.right;
		}
		return res;
	}
}
posted @ 2021-02-26 14:18  ForMei  阅读(50)  评论(0)    收藏  举报