训练营十四天(二叉树的遍历)

二叉树的递归遍历

java 复制代码
树的定义
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    
    public TreeNode() {}
    
    public TreeNode(int val) {
        this.val = val;
    }
    
    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

递归三要素

  • 确定递归函数的参数和返回值
  • 确定终止条件
  • 确定单层递归的逻辑

1.前序遍历 根左右

144.二叉树的前序遍历(opens new window)

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        preorder(root, result);
        return result;
    }

    private void preorder(TreeNode root,  List<Integer> result){
        if(root == null)
            return;
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
}

2.后序遍历 左右根

145.二叉树的后序遍历(opens new window)

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        postorder(root, result);
        return result;
    }

    private void postorder(TreeNode root,  List<Integer> result){
        if(root == null)
            return;
        postorder(root.left,result);
        postorder(root.right,result);
        result.add(root.val);
    }
}

3. 中序遍历 左根右

94.二叉树的中序遍历

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        inorder(root, result);
        return result;
    }

    private void inorder(TreeNode root,  List<Integer> result){
        if(root == null)
            return;
        inorder(root.left,result);
        result.add(root.val);
        inorder(root.right,result);
    }
}

二叉树的迭代遍历

1.前序遍历 根左右

使用栈,因为栈是后入先出,所以使用栈来记录结点,先押入跟结点,在押入右结点和左节点,按照出栈顺序访问元素

java 复制代码
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 temp = stack.pop();
			//left后入栈会先出栈,因为是根左右
			result.add(temp.val);
			if (temp.right != null)
				stack.push(temp.right);
			if (temp.left != null)
				stack.push(temp.left);
		}
		return result;
    }
}

2.后序遍历 左右根

与前序遍历基本一致

  • 后序遍历顺序 左-右-中
  • 入栈顺序:中-左-右
  • 出栈顺序:中-右-左
  • 最后翻转结果(一定要记得翻转结果,否则得到的是根右左)
java 复制代码
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 temp = stack.pop();
			result.add(temp.val);
			if (temp.left != null){
				stack.push(temp.left);
			}
			if (temp.right != null){
				stack.push(temp.right);
			}
		}
		Collections.reverse(result);
		return result;
	}
}

3. 中序遍历 左根右

单独的思路,因为不能先将根压入栈,那么如果先将left入栈会导致指针的丢失,所以要先找到左子树为空的,也就是下一个元素就是当前的栈顶,即当前子树的根结点

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
		List<Integer> result = new ArrayList<>();
		if (root == null)
			return result;
		TreeNode cur = root;
		Stack<TreeNode> stack = new Stack<>();
		while (!stack.isEmpty() || cur != null){
			if (cur != null){//此时要找到左子树的底部
				stack.push(cur);
				cur = cur.left;
			}else {//当前结点没有了左子树,那么当前没有左子树的跟结点就是栈顶
				cur = stack.pop();
				result.add(cur.val);
				cur = cur.right;
			}
		}
		return result;
    }
}
相关推荐
程序员南飞41 分钟前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
弥琉撒到我1 小时前
微服务swagger解析部署使用全流程
java·微服务·架构·swagger
一颗花生米。2 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼2 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
ok!ko5 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2402_857589366 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰6 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没7 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥7 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程8 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统