首页 > 其他分享 >二叉树遍历 前序 中序 后序

二叉树遍历 前序 中序 后序

时间:2023-01-29 19:22:27浏览次数:40  
标签:Node node right root 中序 public 二叉树 data 前序


package com.pay.test; import java.util.LinkedList; public class Node { private Integer data; private Node left; private Node right; public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public Node(Integer data) { this.data = data; } public static Node createNode(LinkedList<Integer> list){ if(list==null || list.isEmpty()){ return null; } Integer data= list.removeFirst(); Node node; if(data!=null ){ node=new Node(data); } return null; } //前序遍历 public void preOrderPrint(Node root){ if(root==null){ return; }else { System.out.println(root.data+""); preOrderPrint(root.left); preOrderPrint(root.right); } } //中序遍历 public void inOrderPrint(Node node){ if(node==null){ return; }else { inOrderPrint(node.left); System.out.println(node.data); inOrderPrint(node.right); } } //后续遍历 public void postOrderPrint(Node node){ if(node==null){ return; }else { postOrderPrint(node.left); postOrderPrint(node.right); System.out.println(node.data); } } }

  测试类

package com.pay.test;

public class Test {

    public static void main(String[] args) {

    Node root=new Node(1);
        Node left=new Node(22);
        Node left1=new Node(44);
        Node left2=new Node(44);

        Node right=new Node(55);
        Node right1=new Node(6);
        Node right2=new Node(7);
        left1.setLeft(left2);
        left.setLeft(left1);
        root.setLeft(left);

        right1.setRight(right2);
        right.setRight(right1);
        root.setRight(right);

        System.out.println("前序遍历");
        root.preOrderPrint(root);

        System.out.println("中序遍历");
        root.inOrderPrint(root);

        System.out.println("后序遍历");
        root.postOrderPrint(root);


    }
}

  

 

标签:Node,node,right,root,中序,public,二叉树,data,前序
From: https://www.cnblogs.com/langjunnan/p/17073647.html

相关文章

  • 力扣110 平衡二叉树
    题目:给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。示例:输入:root......
  • 力扣222 完全二叉树
    题目:给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面......
  • 代码随想录算法训练营第15天 | 二叉树的层序遍历226. 翻转二叉树 101.对称二叉树
    102.二叉树的层序遍历文章:代码随想录(programmercarl.com)视频:讲透二叉树的层序遍历|广度优先搜索|LeetCode:102.二叉树的层序遍历_哔哩哔哩_bilibili思路:层序遍......
  • 代码随想录算法训练营day14 | leetcode 层序遍历 226.翻转二叉树 101.对称二叉树 2
    层序遍历/***二叉树的层序遍历*/classQueueTraverse{/***存放一层一层的数据*/publicList<List<Integer>>resList=newArrayList<>()......
  • 257. 二叉树的所有路径
    问题描述https://leetcode.cn/problems/binary-tree-paths/description/解题思路叶子结点时,添加到结果序列即可。代码#Definitionforabinarytreenode.#class......
  • 226. 反转二叉树
    问题描述https://leetcode.cn/problems/invert-binary-tree/description/解题思路没啥好说的,python的交换简单极了。代码#Definitionforabinarytreenode.#cl......
  • 144. 二叉树的前序遍历
    问题描述https://leetcode.cn/problems/binary-tree-preorder-traversal/description/解题思路二叉树的先序遍历,没啥好说的。中-左-右。先序中序后序说的是中在哪里。......
  • 145. 二叉树的后序遍历
    问题描述https://leetcode.cn/problems/binary-tree-postorder-traversal/description/解题思路这个题和先序一样,没啥好说的。代码#Definitionforabinarytreen......
  • 翻转二叉树
    /***注意:left/right值若没有显示设置为null,值即为undefined*在调用二叉树前、中、后序遍历方法时,由于参数设置了默认值(tree)*所以进入了死循环*/consttree={......
  • 111. 二叉树的最小深度
    问题描述https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/解题思路这个题目不难,但对退出条件要求高。经过对题意的分析,我们对于root为None的......