树的相关定义及遍历
树的一些概念:
两种特殊的二叉树:
满二叉树和完全二叉树:(参考博客)https://blog.csdn.net/u013812939/article/details/46798743
树的遍历
快速写出树的各种遍历结果:
(参考博客:https://blog.csdn.net/WUZHU2017/article/details/108734282)
以上资料来源:《极客时间 | 数据结构与算法之美》
补充一波关于树的前中后序遍历代码,其实是leetcode上的,只要定义好了树的每个节点,就可以做到一鱼多吃。
后序遍历:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
/** * 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> postorderTraversal(TreeNode root) { return postOrder(root); } public List<Integer> postOrder(TreeNode node){ List<Integer> results = new ArrayList<>(); if(node==null){ return results; } results.addAll(postOrder(node.left)); results.addAll(postOrder(node.right)); results.add(node.val); return results; } }